加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 检查UIScrollView中的滚动方向

发布时间:2020-12-15 01:44:08 所属栏目:百科 来源:网络整理
导读:我正在尝试实现scrollViewWillBeginDragging方法. 调用此方法时,我会检查用户是否已选择其中一个滚动视图中的按钮处于状态:已选中. 如果没有,那么我显示UIAlert以通知用户. 我的问题是,如果用户从右向左滚动(从右侧拉下一个视图),我只想调用按钮选择(NextQu
我正在尝试实现scrollViewWillBeginDragging方法.
调用此方法时,我会检查用户是否已选择其中一个滚动视图中的按钮处于状态:已选中.
如果没有,那么我显示UIAlert以通知用户.

我的问题是,如果用户从右向左滚动(从右侧拉下一个视图),我只想调用按钮选择(NextQuestion)方法.
但是,如果他们从左向右滚动,那么我希望它正常滚动.

目前无论用户在哪个方向滚动,都会调用checker方法.如何在从右到左滚动时调用方法?

以下是我目前的实现方式:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self NextQuestion:scrollView];
}

-(IBAction)NextQuestion:(id)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth) / pageWidth) + 1;
    NSInteger npage = 0;
    CGRect frame = scrollView.frame;

    // Check to see if the question has been answered. Call method from another class.
    if([QuestionControl CheckQuestionWasAnswered:page])
    {
        pageNumber++;

        NSLog(@"Proceed");
       if(([loadedQuestionnaire count] - 1) != page)
       {
           [self loadScrollViewWithPage:page - 1];
           [self loadScrollViewWithPage:page];
           [self loadScrollViewWithPage:page + 1];
       }

       // update the scroll view to the appropriate page
       frame = scrollView.frame;
       frame.origin.x = (frame.size.width * page) + frame.size.width;
       frame.origin.y = 0;
       [self.scrollView scrollRectToVisible:frame animated:YES];

   }

   // If question has not been answered show a UIAlert with instructions.
   else
   {
       UIAlertView *alertNotAnswered = [[UIAlertView alloc] initWithTitle:@"Question Not Answered" 
                                                                 message:@"You must answer this question to continue the questionnaire." 
                                                                delegate:nil 
                                                       cancelButtonTitle:@"OK" 
                                                       otherButtonTitles:nil,nil];
       [alertNotAnswered show];
   }
}

解决方案1的代码:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x < lastOffset) // has scrolled left..
    {
        lastOffset = scrollView.contentOffset.x;
        [self NextQuestion:scrollView];
    }
}

解决方法

在scrollViewWillBeginDragging中,滚动视图尚未移动(或注册移动),因此contentOffset将为0.从IOS 5开始,您可以在scrollview的panGestureRecognizer中查看用户滚动手势的方向和大小.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{ 
    CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];

    if(translation.x > 0)
    {
        // react to dragging right
    } else
    {
        // react to dragging left
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读