xcode 检查 UIScrollView 中的滚动方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9637203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:34:33  来源:igfitidea点击:

Check direction of scroll in UIScrollView

iphoneiosxcodeuiscrollviewdirection

提问by HGomez

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.

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

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 Rightthen I want it to scroll as normal.

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

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?

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

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

Code for Solution 1:

解决方案 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];
    }
}

回答by Matt Wielbut

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

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
    }
}

回答by Shubhank

Make a CGFloat lastOffsetas a member variablein your class.hfile..

在你的class.h文件中制作一个CGFloat lastOffsetas a ..member variable

then set it 0 in viewDidLoad.

然后在viewDidLoad.

then check in

然后入住

        - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

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

}

回答by Javier C

You can keep a starting offset as a member of your class which you store when the delegate receives the scrollViewDidBeginDragging:message. Once you have that value you can compare the xvalue of the scroll view offset with the one you have stored and see whether the view was dragged left or right.

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

If changing direction mid-drag is important, you can reset your compared point in the viewDidScroll:delegate method. So a more complete solution would store both the last detected direction and the base offset point and update the state every time a reasonable distance has been dragged.

如果改变方向中拖很重要,您可以在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;
    }
}

The 'reasonable distance' is whatever you need to prevent the scrolling direction to flip between left and right to easily but about 10 points should be about enough.

“合理距离”是防止滚动方向在左右之间轻松翻转所需的任何值,但大约 10 个点应该足够了。