I am have a lot of difficulty trying to create a UICollectionView like in Spotify's Player that acts like this:
The problem for me is two fold.
1) How do I center the cells so that you can see the middle cell as well as the one of the left and right.
- If I create cells that are square and add spacing between each cell, the cells are correctly displayed but are not centered.
2) With pagingEnabled = YES, the collectionview correctly swipes from one page to another. However, without the cells being centered, it simply moves the collection view over a page which is the width of the screen. So the question is how do you make the pages move so you get the effect above.
3) how do you animate the size of the cells as they move
- I don't want to worry about this too much. If I can get that to work it would be great, but the harder problems are 1 and 2.
The code I have currently is a simple UICollectionView with normal delegate setup and custom UICollectionview cells that are squares. Maybe I neeed to subclass UICollectionViewFlowLayout? Or maybe I need to turn pagingEnabled to NO and then use custom swipe events? Would love any help!
In order to create an horizontal carousel layout, you'll have to subclass
UICollectionViewFlowLayout
then overridetargetContentOffset(forProposedContentOffset:withScrollingVelocity:)
,layoutAttributesForElements(in:)
andshouldInvalidateLayout(forBoundsChange:)
.The following Swift 4.1 - iOS 11 complete code shows how to implement them.
CollectionViewController.swift
ZoomAndSnapFlowLayout.swift
CollectionDataSource.swift
CollectionViewCell.swift
Expected result:
Source:
pagingEnabled
should not be enabled as it needs each cell to be the width of you view which will not work for you since you need to see the edges of other cells. For your points 1 and 2. I think you'll find what you need here from one of my late answers to another question.The animation of the cell sizes can be achieved by subclassing UIcollectionviewFlowLayout and overriding
layoutAttributesForItemAtIndexPath:
Within that modify the layout attributes provided by first calling super and then modify the layout attributes size based on the position as it relates to the window centre.Hopefully this helps.
As you have said in the comment you want that in the Objective-c code, there is a very famous library called iCarousel which can be helpful in completing your requirement.Link: https://github.com/nicklockwood/iCarousel
You may use 'Rotary' or 'Linear' or some other style with little or no modification to implement the custom view
To implement it you have implement only some delegate methods of it and it's working for ex:
You can check the full working demo from 'Examples/Basic iOS Example' which is included with the Github repository link
As it is old and popular you can find some related tutorials for it and it will also be much stable than the custom code implementation
Well, I made UICollectionview moving just like this, yesterday.
I can share my code with you :)
Here's my storyboard
make sure uncheck 'Paging Enabled'
Here's my code.
Key code of make cell centered is
scrollViewWillEndDragging
insetForSectionAtIndex
Key code of animate the size is
I wish this helps you
P.S. If you want to change alpha just like the image that you uploaded, add [cell setalpha] in scrollViewDidScroll
I wanted similar behavior a little while back, and with the help of @Mike_M I was able to figure it out. Although there are many, many way to do this, this particular implementation is to create a custom UICollectionViewLayout.
Code below(gist can be found here: https://gist.github.com/mmick66/9812223)
Now it's important to set the following:
*yourCollectionView*.decelerationRate = UIScrollViewDecelerationRateFast
, this prevents cells being skipped by a quick swipe.That should cover part 1 and 2. Now, for part 3 you could incorporate that in the custom collectionView by constantly invalidating and updating, but it's a bit of a hassle if you ask me. So another approach would be to to set a
CGAffineTransformMakeScale( , )
in theUIScrollViewDidScroll
where you dynamically update the cell's size based on it's distance from the center of the screen.You can get the indexPaths of the visible cells of the collectionView using
[*youCollectionView indexPathsForVisibleItems]
and then getting the cells for these indexPaths. For every cell, calculate the distance of its center to the center of yourCollectionViewThe center of the collectionView can be found using this nifty method:
CGPoint point = [self.view convertPoint:*yourCollectionView*.center toView:*yourCollectionView];
Now set up a rule, that if the cell's center is further than x away, the size of the cell is for example the 'normal size', call it 1. and the closer it gets to the center, the closer it gets to twice the normal size 2.
then you can use the following if/else idea:
What happens is that the cell's size will exactly follow your touch. Let me know if you have any more questions as I'm writing most of this out of the top of my head).