5

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

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

目前,无论用户向哪个方向滚动都会调用 checker 方法。我怎样才能只在他们从Right To Left滚动时调用一个方法?

这是我目前实现它的方式:

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

3 回答 3

20

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
    }
}
于 2013-10-17T09:40:56.003 回答
0

在您的class.h文件中创建一个CGFloat lastOffsetas a ..member variable

然后将其设置为 0 viewDidLoad

然后签到

        - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

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

}
于 2012-03-09T16:13:36.193 回答
0

当委托接收到scrollViewDidBeginDragging:消息时,您可以将起始偏移量保留为您存储的类的成员。获得该值后,您可以将滚动视图偏移的x值与您存储的值进行比较,并查看视图是向左还是向右拖动。

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

- (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 个点应该足够了。

于 2012-03-09T21:14:01.673 回答