iphone - 检查 UIScrollView 中的滚动方向

标签 iphone ios xcode uiscrollview direction

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

我的问题是,如果用户从从右到左滚动(从右侧拉出下一个 View ),我只想调用已选择的按钮 (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];
   }
} 

解决方案 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 中, ScrollView 尚未移动(或注册移动),因此 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
    }
}

关于iphone - 检查 UIScrollView 中的滚动方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9637203/

相关文章:

iphone - 如何在仍然有添加/删除按钮的情况下阻止 tableview 缩进编辑?

iphone - 为什么 CorePlot 0.2.2 对于模拟器构建良好,但对于设备却给出链接错误?

IOS队列异步下载

ios - UISegmentedControl 在 UITableView 标题上的行为方式很奇怪

iphone - iOS 7 - 没有配置的 ios 设备可用

iphone - 苹果集团崩溃报告如何?

ios - 为什么,将 nil 作为参数从 Objc C 发送到 swift 类初始值设定项,用新对象替换 nil 参数

iphone - 多个同时发生的 UILocalNotification

objective-c - 如何将 textField 复制到 OSX 剪贴板?

ios - 基于 4 英寸显示屏构建的设备不显示黑色顶部和底部栏