iOS:轻扫手势以在同一控制器中加载不同的数据

时间:2014-04-18 10:44:16

标签: ios objective-c uigesturerecognizer

在我的应用程序中,我试图实现一个功能(类似于同一视图中月份更改的日历),如果当前视图显示当天(今天)的新闻,那么如果用户向右滑动,它应该显示第二天新闻和刷卡离开它应该显示前一天的新闻。

I have so far

  

单个NewsViewController,在表格视图中显示今日新闻

关于其他主题,许多建议使用类似于这个问题的库汤姆建议Handling a view controller that slides in with either way swipe 我是IOS开发的新手,不知道怎么做,任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

因此,您可以尝试使用此鳕鱼https://stackoverflow.com/a/20596933/1307844

在任何一种手势方法中提取数据

您的答案完全是您实施手势方法的方法。

答案 1 :(得分:0)

我找到了解决方案,这是我的代码片段......

<强烈> viewDidLoad中

UISwipeGestureRecognizer *oneFingerSwipeLeft = [[UISwipeGestureRecognizer alloc]
                                                initWithTarget:self
                                                action:@selector(oneFingerSwipeLeft:)];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[viewContent addGestureRecognizer:oneFingerSwipeLeft];

UISwipeGestureRecognizer *oneFingerSwipeRight = [[UISwipeGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(oneFingerSwipeRight:)];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[viewContent addGestureRecognizer:oneFingerSwipeRight];

<强> oneFingerSwipeLeft

- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer {
newsList = [[NSMutableArray alloc] init];

//to animate the view as new view is loaded
[UIView animateWithDuration:0.1 animations:^{

    viewContent.frame = CGRectMake( -viewContent.frame.size.width, viewContent.frame.origin.y , viewContent.frame.size.width, viewContent.frame.size.height);
    [self loadData];

} completion:^(BOOL finished) {
    viewContent.frame = CGRectMake( viewContent.frame.size.width,viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);

    [UIView animateWithDuration:0.3 animations:^{
        viewContent.frame = CGRectMake(0.0, viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);
    }];
}];

selectedDay++;
[self fetchDataFromWeb];

}

<强> oneFingerSwipeRight

- (void)oneFingerSwipeRight:(UITapGestureRecognizer *)recognizer {
newsList = [[NSMutableArray alloc] init];

//to animate the view as new view is loaded
[UIView animateWithDuration:0.1 animations:^{

    viewContent.frame = CGRectMake( viewContent.frame.size.width, viewContent.frame.origin.y , viewContent.frame.size.width, viewContent.frame.size.height);
    [self loadData];

} completion:^(BOOL finished) {
    viewContent.frame = CGRectMake( -viewContent.frame.size.width,viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);

    [UIView animateWithDuration:0.3 animations:^{
        viewContent.frame = CGRectMake(0.0, viewContent.frame.origin.y, viewContent.frame.size.width, viewContent.frame.size.height);
    }];
}];

selectedDay--;
[self fetchDataFromWeb];

}

感谢@BalramTiwari提供宝贵的建议。

答案 2 :(得分:0)

这是IceFish在swift中的答案:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeft)

和响应者:

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Left:
            UIView.animateWithDuration(0.1, animations: {
                let myFrame = self.view.frame
                self.view.frame = CGRectMake(-myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
                }, completion: {_ in
                    let myFrame = self.view.frame
                    self.view.frame = CGRectMake(myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)

                    UIView.animateWithDuration(0.3, animations: {
                        let myFrame = self.view.frame
                        self.view.frame = CGRectMake(0.0, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
                        }, completion: nil)
            })
            // do left action here

        case UISwipeGestureRecognizerDirection.Right:
            UIView.animateWithDuration(0.1, animations: {
                let myFrame = self.view.frame
                self.view.frame = CGRectMake(myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
            }, completion: {_ in
                let myFrame = self.view.frame
                self.view.frame = CGRectMake(-myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)

                UIView.animateWithDuration(0.3, animations: {
                    let myFrame = self.view.frame
                    self.view.frame = CGRectMake(0.0, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
                }, completion: nil)
            })
            // do right action here

        default:
            break
        }
    }
}