I'm working on a project where I'm using a UICollectionView to create an 'image ticker' where I'm advertising a series of logos. The collectionView is one item high and twelve items long, and shows two to three items at a time (depending on size of the logos visible).
I would like to make a slow automatic scrolling animation from the first item to the last, and then repeat.
Has anyone been able to make this work? I can get the scrolling working using
[myCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myImages.count -1) inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
But this is way too fast!
[UIView animateWithDuration:10 delay:2 options:(UIViewAnimationOptionAutoreverse + UIViewAnimationOptionRepeat) animations:^{
[myCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(myImages.count -1) inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
} completion:nil];
This yields the desired scrolling speed, but only the last few cells are visible in the series. I suspect they (and even the starting visible cells) are being dequeued immediately.
Any thoughts?
You could also have a "slow" scroll to the end of you
UICollectionView
without having to "jump" from indexpath to indexpath. I created this quickly for a collection view on aTVOS
app:I just call the
autoScroll()
in theviewDidLoad()
and the rest takes care of it itself. The speed of the scrolling is decided by the animation time of theUIView
. You could (I haven't tried) add an NSTimer with0
seconds instead so you can invalidate it on userscroll.You can try this approach:
For anyone else finding this, I've updated Masa's suggestion to work on Swift and I've introduced a little bit of easing towards the end so it acts more like the original scrollItemsToIndexPath animated call. I have hundreds of items in my view so a steady pace wasn't an option for me.
Tweaking the values of dif and modifier adjusts the duration/level of ease for most situations.
I use a "1 pixel offset" trick. When scrolling programmatically, just set the end
contentOffset.x
to 1 pixel more/less than it should. This should be unnoticeable. And in completion block set it to actual value. That way you can avoid the premature cell dequeuing problem and get smooth scrolling animation ;)Here is an example of scrolling to the right page (e.g. from page 1 to 2). Notice that in the
animations:
block I actually scrolls one pixel less (pageWidth * nextPage - 1
). I then restore the correct value in thecompletion:
block.In .h file, declare this timer,
Place this timer code,
then,
Hope it'll be useful.