Reputation: 4503
A user performs a quick swipe gesture to make a UICollectionView start scrolling (it will gradually come to an halt).
How can I programmatically force the scrolling to come to an immediate stop? To clarify, I want to allow the deceleration but I need to be able to stop it in code.
Upvotes: 19
Views: 37424
Reputation: 560
if you have the pagingEnabled
and scrollEnabled
properties set to true
than this should work:
self.collectionView.scrollEnabled = false
self.collectionView.pagingEnabled = false
Upvotes: 6
Reputation: 1210
Try this one. Worked for me. :)
self.collectionView.scrollEnabled = NO;
Upvotes: 36
Reputation: 12678
Have you tried the following?
[self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO];
the contentOffset
property is constantly updated as the collectionView scrolls (even via animation) so at the time of calling the above, it should hopefully force the collectionView to stop its existing animation.
Upvotes: 36
Reputation: 13549
Adopt the following scrollViewDelegate
method to pick up when the user lets go of dragging the collectionView.
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset;
You can then just create your own animation block to set whichever speed/final destination you think looks best using the contentOffset
property.
Upvotes: 1