21

I did google enough, & I did check posts like these ( Finding the direction of scrolling in a UIScrollView? ) in stackoverflow before posting this. I have a dynamic number of photos in an iPhone App, that am displaying through UIScrollView. At any point in time, I have only 3 photos being displayed in the scroll-view. When I have, say 4 photos, in total: 1st photo : displayed at offset 0.0 2nd photo : displayed at offset 320.0 3rd photo : displayed at offset 640.0

Now, when the user scrolls to the 4th photo, the scroll-view resets to 0.0 offset. If the user tries to scroll 'beyond' the 4th photo, scrolling should stop in the right-direction only (so that user doesn't scroll 'beyond'). But currently, the user 'is able' to scroll beyond the last photo ; however, I detect this programmatically & reset the offset. But it doesn't look neat, as the user sees the black background momentarily. I want to detect that the user has started scrolling 'right' (remember, scrolling 'left' i.e. to the 'previous' photo is okay) in scrollViewWillBeginDragging, so that I can stop any further scrolling to the right.

What I tried:

  1. Trying using self.scrollView.panGestureRecognizer's translationInView isn't working, because there is no panGestureRecognizer instance returned in the first place (!), though the UIScrollView API claims so.
  2. Detecting this in scrollViewDidEndDecelerating is possible, though it'll not serve my purpose.
Community
  • 1
  • 1
Jean
  • 2,457
  • 6
  • 32
  • 58

9 Answers9

28

I had no issues determining direction in scrollViewWillBeginDragging when checking the scroll view's panGestureRecognizer:

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

    if(translation.y > 0)
    {
        // react to dragging down
    } else
    {
        // react to dragging up
    }
}

I found it very useful in canceling out of a scroll at the very first drag move when the user is dragging in a forbidden direction.

Matt Wielbut
  • 2,276
  • 21
  • 26
13

Swift 3 Solution

1- Add UIScrollViewDelegate

2- Add this code

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    let actualPosition = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
    if (actualPosition.y > 0){
        // Dragging down
    }else{
        // Dragging up
    }
}
Oscar Falmer
  • 1,591
  • 1
  • 20
  • 34
12

For swift 2.0+ & ios 8.0+

func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
    let actualPosition = scrollView.panGestureRecognizer.translationInView(scrollView.superview)
    if (actualPosition.y > 0){
        // Dragging down 
    }else{
        // Dragging up
    }
}
PAC
  • 1,374
  • 1
  • 16
  • 23
6

Thank you, Kris. This is what worked for me, finally:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Detect the scroll direction
    if (lastContentOffset < (int)scrollView.contentOffset.x) {
    ...
    }
}
Aaron Brager
  • 62,532
  • 17
  • 146
  • 267
Jean
  • 2,457
  • 6
  • 32
  • 58
  • 2
    Could you elaborate on lastContentOffset – Daniel Jan 16 '14 at 01:04
  • 1
    @Daniel you need to save the "last content offset", which is the previous contentOffset from the last time this method was called. I don't know why Jean forgot it. Just add lastContentOffset as static variable at the top, then set it at the end of the method. – Michael Ozeryansky Mar 01 '15 at 09:18
5

This is what I used and it works nicely at least on iOS 6.0:

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

   // Detect direction by accessing x or y of translation
}

Saves you the instance variable for lastContentOffset ...

Michael Thiel
  • 2,394
  • 21
  • 20
1

Take a look at scrollViewWillEndDragging:withVelocity:targetContentOffset:. You can use that method do do any checking and see if it is going where it should not and then in the method you can set a new targetContentOffset.

Per the documentation:

This method is not called when the value of the scroll view’s pagingEnabled property is YES. Your application can change the value of the targetContentOffset parameter to adjust where the scrollview finishes its scrolling animation.

Aaron Brager
  • 62,532
  • 17
  • 146
  • 267
Kris Gellci
  • 8,943
  • 5
  • 35
  • 47
1

There seem to be issues with detecting scroll direction based on the translation of the scrollView's pan recognizer in iOS 7+. This seems to be working pretty seamlessly for my purposes

func scrollViewDidScroll(scrollView: UIScrollView) {
    if !scrollDirectionDetermined {
        let translation = scrollView.panGestureRecognizer.translationInView(self.view)
        if translation.y > 0 {
            println("UP")
            scrollDirectionDetermined = true
        }
        else if translation.y < 0 {
            println("DOWN")
            scrollDirectionDetermined = true
        }
    }
}

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    scrollDirectionDetermined = false
}

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    scrollDirectionDetermined = false
}
wfbarksdale
  • 6,962
  • 11
  • 60
  • 87
0

scrollView.panGestureRecognizer translationInView:scrollView doesn't report anything useful in scrollViewWillBeginDragging in iOS 7.

This does:

In the @interface

BOOL scrollDirectionDetermined;

and:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!scrollDirectionDetermined) {
        if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x > 0) {
            //scrolling rightwards
        } else {
             //scrolling leftwards
       }
    scrollDirectionDetermined = YES;
    }
}

and:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    scrollDirectionDetermined = NO;
}
0

Building off of @Oscar's answer, you can do things like

scrollView.bounces = actualPosition.y < 0 if you want the scrollView to bounce when you scroll to the bottom but not when you scroll to the top

Zack Shapiro
  • 5,202
  • 14
  • 65
  • 121