检查UIScrollView的滚动方向

我试图实现方法scrollViewWillBeginDragging。 当这个方法被调用时,我检查用户是否select了滚动视图中的一个button处于状态:selected。 如果没有,那么我会显示一个UIAlert通知用户。

我的问题是我只想调用buttonselect( 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]; } } 

解决scheme1的代码:

 - (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将会减less0.从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 } } 

CGFloat lastOffset作为你的class.h文件中的一个member variable

然后在viewDidLoad设置为0。

然后登记

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

您可以保留一个起始偏移作为您的类的成员,当委托收到scrollViewDidBeginDragging:消息时,您存储的成员。 一旦你有了这个值,你可以比较滚动视图偏移的X值和你已经存储的值,看看是否向左或向右拖动视图。

如果改变中间拖动的方向很重要,可以在viewDidScroll: delegate方法中重置您的比较点。 因此,更完整的解决scheme将存储最后检测到的方向和基准偏移点,并在每次拖动合理的距离时更新状态。

 - (void) scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat distance = lastScrollPoint.x - scrollView.contentOffset.x; NSInteger direction = distance > 0 ? 1 : -1; if (abs(distance) > kReasonableDistance && direction != lastDirection) { lastDirection = direction; lastScrollPoint = scrollView.contentOffset; } } 

“合理的距离”是防止滚动方向在左右之间轻松翻转所需的任何东西,但是约10分就足够了。