Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:滑动手势以在同一控制器中加载不同的数据_Ios_Objective C_Uigesturerecognizer - Fatal编程技术网

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

iOS:滑动手势以在同一控制器中加载不同的数据,ios,objective-c,uigesturerecognizer,Ios,Objective C,Uigesturerecognizer,在我的应用程序中,我试图实现一种功能(类似于日历,在同一视图中月份会发生变化),比如如果当前视图显示当天(今天)的新闻,那么如果用户向右滑动,它将显示第二天的新闻,向左滑动时,它将显示前一天的新闻 到目前为止我有 在表视图中显示今日新闻的单个NewsViewController 在其他问题上,许多人建议使用类似于Tom建议的库 我是IOS开发新手,不知道如何做,非常感谢您的帮助。因此,您可以尝试使用此代码在任何手势方法中提取数据 您的答案完全是如何实现手势方法。我找到了解决方案,下面是我的代码片

在我的应用程序中,我试图实现一种功能(类似于日历,在同一视图中月份会发生变化),比如如果当前视图显示当天(今天)的新闻,那么如果用户向右滑动,它将显示第二天的新闻,向左滑动时,它将显示前一天的新闻

到目前为止我有

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

在其他问题上,许多人建议使用类似于Tom建议的库
我是IOS开发新手,不知道如何做,非常感谢您的帮助。

因此,您可以尝试使用此代码在任何手势方法中提取数据


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

我找到了解决方案,下面是我的代码片段

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];
- (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];
- (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];
OneFingersSwipeleft

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];
- (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];
- (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];
}

oneFingerSwipeRight

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

以下是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
        }
    }
}

查看此链接,感谢您的回复,但如何在滑动时显示具有不同数据的相同viewController。这意味着相同视图将对新的web服务进行迭代。你有任何在线教程链接吗?这说明了如何做到这一点?@MuhammadUmair:根据您的实现,有许多可能性。对于一般情况,假设您正在服务调用的
UILabel
中显示新闻数据。因此,如果您向左滑动,请加载第二天数据的服务呼叫。如果手势是向右滑动,则使用服务呼叫获取前一天的数据。我相信,您一定有关于如何呼叫服务或读取提要的信息。如果你把你的问题分成小块,比如服务电话、数据表示、手势,然后重新加入所有的PEICE,那就更好了。