I\'m trying to limit the scroll speed of my UITableView, exactly like Instagram does it.
If you check out Instagram, you\'ll notice that they have a limit on how fast yo
TableView has a property scrollView
, This property will return internal scrollView of TableView. Use following...
tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
ANOTHER WAY:
TableView will respond to scrollView delegate, so we need to implement scrollView's delegate like:
Take these global variables :
CGPoint lastOffset;
NSTimeInterval lastOffsetCapture;
BOOL isScrollingFast;
Implement scrollViewDidScroll
like :
- (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;
}
}
Then implement scrollViewDidEndDragging
like this :
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if( !decelerate )
{
NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);
[scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES];
}
}
Hope this may help you...
Use this:
self.tableview.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
As tableView is a subclass of UIScrollView, ScrollView delegate will work here. Hope this helps.. :)
Edit:
if tableView doesn't show scrollView property use:
self.tableView.decelerationRate