Check direction of scroll in UIScrollView
Asked Answered
H

3

5

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.

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

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?

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:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x < lastOffset) // has scrolled left..
    {
        lastOffset = scrollView.contentOffset.x;
        [self NextQuestion:scrollView];
    }
}
Henka answered 9/3, 2012 at 16:5 Comment(1)
Have a look at this answer: https://mcmap.net/q/127590/-finding-the-direction-of-scrolling-in-a-uiscrollviewTova
B
20

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

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{ 
    CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];

    if(translation.x > 0)
    {
        // react to dragging right
    } else
    {
        // react to dragging left
    }
}
Bilbao answered 17/10, 2013 at 9:40 Comment(2)
Note, the translation can return zero if the gesture is too subtle. Use CGPoint velocity = [scrollView.panGestureRecognizer velocityInView:scrollView.superview]; instead.Heddle
Furthermore note, if you flick a UIScrollView and then touch and hold while the view is still decelerating, scrollViewWillBeginDragging will be called with translation and velocity both having x = y = 0. So better do not assume that else{…} would be dragging left. Check for translation/velocity values of zero to be sure.Heddle
B
0

Make a CGFloat lastOffset as a member variable in your class.h file..

then set it 0 in viewDidLoad .

then check in

        - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

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

}
Benuecongo answered 9/3, 2012 at 16:13 Comment(7)
Unfortunately this did not work. Debugger show it to skip this If statement. I have set lastOffset to 0 in ViewDidLoad. And Ideas?Henka
NSLog scrollView.contentOffset.x just before if statement .. it should be negative for left scrollingBenuecongo
Scrolling Right to Left shows scrollview.contentOffset.x at 0.000000 . Left to right shows it at 768.000000Henka
Those are the outputs from my NSLogHenka
ok the problem is with your delegate method ./..change the delegate method to - (void)scrollViewDidScroll:(UIScrollView *)scrollView Benuecongo
Im sorry but I cannot use the ViewDidScroll method as I need to detect when the user starts dragging in order to load the next subview =[Henka
you won't be able to do that way,,since will scrolling is called.before actual scrolling report.. it is just an indication to you that it will scroll..finding the actual scroll view is done in the didScroll method onlyBenuecongo
A
0

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 x value of the scroll view offset with the one you have stored and see whether the view was dragged left or right.

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.

- (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.

Amblygonite answered 9/3, 2012 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.