Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4503

Force UICollectionView to stop scrolling

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

Answers (5)

Idan Yehuda
Idan Yehuda

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

Rakesh Gujari
Rakesh Gujari

Reputation: 161

For Swift 3:

collectionView.isScrollEnabled = false

Upvotes: 14

haifacarina
haifacarina

Reputation: 1210

Try this one. Worked for me. :)

self.collectionView.scrollEnabled = NO;

Upvotes: 36

Liam
Liam

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

Tommy Devoy
Tommy Devoy

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

Related Questions