IOS - Check direction of scroll in UIScrollView?

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

Up vote 2 down vote favorite share g+ share fb share tw.

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 X; self NextQuestion:scrollView; } } iphone ios xcode uiscrollview direction link|improve this question edited Mar 9 at 16:53 asked Mar 9 at 16:05Rupert3798 88% accept rate.

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.

Unfortunately this did not work. Debugger show it to skip this If statement. I have set lastOffset to 0 in ViewDidLoad.

And Ideas? – Rupert Mar 9 at 16:27 NSLog scrollView.contentOffset. X just before if statement .. it should be negative for left scrolling – Shubhank Mar 9 at 16:34 Scrolling Right to Left shows scrollview.contentOffset.

X at 0.000000 . Left to right shows it at 768.000000 – Rupert Mar 9 at 16:39 it does not show negative offset ..? tell your slog statement.. – Shubhank Mar 9 at 16:41 Those are the outputs from my NSLog – Rupert Mar 9 at 16:48.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions