39

I know how to get the contentOffset on movement for a UIScrollView, can someone explain to me how I can get an actual number that represents the current speed of a UIScrollView while it is tracking, or decelerating?

rolling_codes
  • 15,174
  • 22
  • 76
  • 112

8 Answers8

85

There's an easier way: check the UISCrollview's pan gesture recognizer. With it, you can get the velocity like so:

CGPoint scrollVelocity = [[_scrollView panGestureRecognizer] velocityInView:self];
karstux
  • 1,482
  • 11
  • 8
  • 28
    Handy! The only downside is that `scrollVelocity` will be 0.0f immediately after the user lifts their finger (because this ends the panning gesture). So it's a good way to measure velocity while the scrollview is being dragged, but doesn't work if flicked. – Kyle Fox Apr 09 '12 at 17:33
  • 2
    ..and of course the panGestureRecognizer is only exposed in iOS 5 onwards. – bandejapaisa Aug 20 '12 at 11:00
  • is the scroll velocity a point because it records vertical and horizontal speeds? – rolling_codes Jul 30 '14 at 13:11
  • 1
    to what @KyleFox said, yes of course true, make use of `scrollViewWillEndDragging:withVelocity:targetContentOffset:` to perhaps handle and velocity or offset conditions AFTER the current touch event ends – Will Von Ullrich Jun 01 '17 at 19:32
  • 1
    That's the velocity of the touch input, not the scrollView. – Robin Jan 08 '19 at 08:24
60

Have these properties on your UIScrollViewDelegate

CGPoint lastOffset;
NSTimeInterval lastOffsetCapture;
BOOL isScrollingFast;

Then have this code for your scrollViewDidScroll:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {    
    CGPoint currentOffset = scrollView.contentOffset;
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];

    NSTimeInterval timeDiff = currentTime - lastOffsetCapture;
    if(timeDiff > 0.1) {
        CGFloat distance = currentOffset.y - lastOffset.y;
        //The multiply by 10, / 1000 isn't really necessary.......
        CGFloat scrollSpeedNotAbs = (distance * 10) / 1000; //in pixels per millisecond

        CGFloat scrollSpeed = fabsf(scrollSpeedNotAbs);
        if (scrollSpeed > 0.5) {
            isScrollingFast = YES;
            NSLog(@"Fast");
        } else {
            isScrollingFast = NO;
            NSLog(@"Slow");
        }        

        lastOffset = currentOffset;
        lastOffsetCapture = currentTime;
    }
}

And from this i'm getting pixels per millisecond, which if is greater than 0.5, i've logged as fast, and anything below is logged as slow.

I use this for loading some cells on a table view animated. It doesn't scroll so well if I load them when the user is scrolling fast.

djromero
  • 19,551
  • 4
  • 71
  • 68
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • 1
    brilliant, this was exactly what i was planning. – jfisk Aug 16 '12 at 19:28
  • 4
    thanks for a nice solution, the only thing I'd change is replace 0.1 with `captureInterval` const and than use it also in calculation of `scrollSpeedNotAbs` to increase readability of the algorithm (as the brain stops on `* 10` because general formula of speed is distance / time). – zubko Oct 21 '12 at 19:20
  • 2
    wonderful extension to the scrollview / collectionView ! Saved my day :-) – PetrV Jul 19 '13 at 12:21
  • While this is not incorrect, I put in the modern solution to this very old question down the bottom. Cheers – Fattie Jul 14 '17 at 10:29
  • @Fattie I just tried this and it seems to work fine. Why did you say that it's not correct – Lance Samaria Mar 06 '22 at 18:46
  • @LanceSamaria I said it is ***not*** incorrect, but it is very old fashioned, slow, and complicated, and does not exactly follow how it works in modern iOS. Simply use the far easier solution below. – Fattie Mar 07 '22 at 01:54
17

Converted @bandejapaisa answer to Swift 5:

Properties used by UIScrollViewDelegate:

var lastOffset: CGPoint = .zero
var lastOffsetCapture: TimeInterval = .zero
var isScrollingFast: Bool = false

And the scrollViewDidScroll function:

func scrollViewDidScroll(scrollView: UIScrollView) {

    let currentOffset = scrollView.contentOffset
    let currentTime = Date.timeIntervalSinceReferenceDate
    let timeDiff = currentTime - lastOffsetCapture
    let captureInterval = 0.1
    
    if timeDiff > captureInterval {
        
        let distance = currentOffset.y - lastOffset.y     // calc distance
        let scrollSpeedNotAbs = (distance * 10) / 1000     // pixels per ms*10
        let scrollSpeed = fabsf(Float(scrollSpeedNotAbs))  // absolute value
        
        if scrollSpeed > 0.5 {
            isScrollingFast = true
            print("Fast")
        } else {
            isScrollingFast = false
            print("Slow")
        }
        
        lastOffset = currentOffset
        lastOffsetCapture = currentTime
        
    }
}
Mat0
  • 1,165
  • 2
  • 11
  • 27
12

For a simple speed calculation (All the other answers are more complicated):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollSpeed = scrollView.contentOffset.y - previousScrollViewYOffset;
    previousTableViewYOffset = scrollView.contentOffset.y;
}
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
  • This helped me out as well, thanks! Other people might consider throwing a fabsf() in there, just to get the absolute value. – Andrew Sep 09 '13 at 12:17
  • 3
    You need to take the time into account otherwise the speed will jitter. – meaning-matters Sep 03 '15 at 23:43
  • Other than the jittering issue, the other issue is that if I change the contentInset of the scrollView it will be registered as a fast scroll. I'm having trouble distinguishing between the two events. – John Farkerson Jun 25 '19 at 23:12
8

2017...

It's very easy to do this with modern Swift/iOS:

var previousScrollMoment: Date = Date()
var previousScrollX: CGFloat = 0

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    let d = Date()
    let x = scrollView.contentOffset.x
    let elapsed = Date().timeIntervalSince(previousScrollMoment)
    let distance = (x - previousScrollX)
    let velocity = (elapsed == 0) ? 0 : fabs(distance / CGFloat(elapsed))
    previousScrollMoment = d
    previousScrollX = x
    print("vel \(velocity)")

Of course you want the velocity in points per second, which is what that is.

Humans drag at say 200 - 400 pps (on 2017 devices).

1000 - 3000 is a fast throw.

As it slows down to a stop, 20 - 30 is common.

So very often you will see code like this ..

    if velocity > 300 {
    
        // the display is >skimming<
        some_global_doNotMakeDatabaseCalls = true
        some_global_doNotRenderDiagrams = true
    }
    else {
    
        // we are not skimming, ok to do calculations
        some_global_doNotMakeDatabaseCalls = false
        some_global_doNotRenderDiagrams = false
    }

This is the basis for "skimming engineering" on mobiles. (Which is a large and difficult topic.)

Note that that is not a complete skimming solution; you also have to care for unusual cases like "it has stopped" "the screen just closed" etc etc.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
6

May be this would be helpful

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
FunkyKat
  • 3,233
  • 1
  • 23
  • 20
  • want to mention, this is example from velocity output: `(0.0,-5.35356)`. Scroll by y axis, 5.35.. -> 15 points per `scrollViewDidScroll` calling :) So, 5 is very fast. – Dima Deplov Dec 03 '14 at 20:22
3

You can see PageControl sample code about how to get the contentOffset of scrollview.

The contentOffset on movement can be obtained from UIScrollViewDelegate method, named - (void)scrollViewDidScroll:(UIScrollView *)scrollView, by querying scrollView.contentOffset. Current speed can be calculated by delta_offset and delta_time.

  • Delta_offset = current_offset - pre_offset;
  • Delta_time = current_time - pre_time;
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
1

Here is another smart way to do this in SWIFT :-

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if velocity.y > 1.0 || velocity.y < -1.0 && self.sendMessageView.isFirstResponder() {
        // Somthing you want to do when scrollin fast.
        // Generally fast Vertical scrolling.
    }
}

So if you scrolling vertically you should use velocity.y and also if you are scrolling horizontally you should use velocity.x . Generally if value is more than 1 and less than -1, it represent generally fast scrolling. So you can change the speed as you want. +value means scrolling up and -value means scrolling down.

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58