检查 UIScrollView 中的滚动方向

Check direction of scroll in UIScrollView(检查 UIScrollView 中的滚动方向)

本文介绍了检查 UIScrollView 中的滚动方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现 scrollViewWillBeginDragging 方法.调用此方法时,我检查用户是否选择了滚动视图中的按钮之一处于状态:已选择.如果没有,我会显示一个 UIAlert 来通知用户.

I am trying to implement the method scrollViewWillBeginDragging. When this method is called I check that the user has selected one of the buttons that are within the scrollview is in state:selected. If not then I display a UIAlert to inform the user.

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

My problem here is I only want to call the button selected (NextQuestion) method if the user scrolls from right to left (pulling the next view from the right). But if they are scrolling from Left to Right then I want it to scroll as normal.

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

At present the checker method is called no matter what direction the user scrolls in. How can I only call a method when they scroll from Right To Left?

这是我目前的实现方式:

Here is how i've currently implemented it:

- (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 开始,您可以改为查看滚动视图的 panGestureRecognizer 以确定用户滚动手势的方向和幅度.

In scrollViewWillBeginDragging the scroll view has not yet moved (or registered the move) and so contentOffset will by 0. As of IOS 5 you can instead look in the scrollview's panGestureRecognizer to determine the direction and magnitude of the user's scrolling gesture.

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

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

这篇关于检查 UIScrollView 中的滚动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:检查 UIScrollView 中的滚动方向

基础教程推荐