检查UIScrollView中的滚动方向

HGo*_*mez 5 iphone xcode direction uiscrollview ios

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

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

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

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

- (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];
   }
} 
Run Code Online (Sandbox Code Playgroud)

解决方案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];
    }
}
Run Code Online (Sandbox Code Playgroud)

mwi*_*but 21

scrollViewWillBeginDragging滚动视图中尚未移动(或注册移动),因此contentOffset将为0.从IOS 5开始,您可以在滚动视图中查看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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • **注意,如果手势过于微妙,翻译可以返回零.**使用```CGPoint velocity = [scrollView.panGestureRecognizer velocityInView:scrollView.superview];```而不是. (6认同)

Shu*_*ank 0

在你的class.h文件中将a 作为CGFloat lastOffseta ..member variable

然后将其设置为0 viewDidLoad

然后办理登机手续

        - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

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

}
Run Code Online (Sandbox Code Playgroud)