Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UIScrollView Speed Check

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?

like image 470
NoodleOfDeath Avatar asked Sep 15 '10 16:09

NoodleOfDeath


2 Answers

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]; 
like image 193
karstux Avatar answered Sep 23 '22 04:09

karstux


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.

like image 41
bandejapaisa Avatar answered Sep 26 '22 04:09

bandejapaisa