【问题标题】:iOS: swipe gesture to load different data within same controlleriOS:滑动手势以在同一控制器中加载不同的数据
【发布时间】:2014-04-18 10:44:16
【问题描述】:

在我的应用程序中,我正在尝试实现一个功能(类似于在同一视图中月份更改的日历),例如如果当前视图显示当天(今天)的新闻,那么如果用户向右滑动,它应该显示下一个每日新闻,向左滑动应显示前一日新闻。

I have so far

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

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

【问题讨论】:

标签: ios objective-c uigesturerecognizer


【解决方案1】:

所以你可以在这里https://stackoverflow.com/a/20596933/1307844https://stackoverflow.com/a/20596933/1307844尝试使用任何手势方法提取数据

你的答案完全是你如何实现你的手势方法。

【讨论】:

  • 感谢您的回复,但如何在滑动时显示具有不同数据的相同 viewController。
  • 意味着相同的视图将迭代新的 Web 服务。请问您有任何在线教程链接吗?这说明了如何做到这一点?
  • @MuhammadUmair :有很多可能性取决于您的实现。对于一般情况,假设您正在通过服务调用在UILabel 中显示您的新闻数据。因此,如果您向左滑动,请加载第二天数据的服务调用。如果手势是向右滑动,请使用服务调用获取前一天的数据。我相信,您一定知道如何调用服务或阅读提要。最好将问题分解成小块,例如服务调用、数据表示、手势,然后重新加入所有小块。
【解决方案2】:

我找到了解决方案,这是我的代码 sn-ps...

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 的宝贵建议。

【讨论】:

    【解决方案3】:

    这是 IceFish 的快速回答:

    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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2013-10-15
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多