I have a Collection View that can show about 3.5 cells at a time, and I want it to be paging-enabled. But I'd like it to snap to each cell (just like the App Store app does), and not scroll the full width of the view. How can I do that?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
Here's my implementation in Swift 4.2 for vertical cell-based paging:
Some notes:
itemSize
actually matches the size of the item as that's often a problem.self.collectionView.decelerationRate = UIScollViewDecelerationRateFast
.Here's a horizontal version (haven't tested it thoroughly so please forgive any mistakes):
I developed my solution before looking at the ones here. I also went with creating a custom UICollectionViewFlowLayout and override the targetContentOffset method.
It seems to work fine for me (i.e. I get the same behavior as in the AppStore) even though I got much less code. Here it is, feel free to point me any drawback you can think of:
This is my solution. Works with any page width.
Set
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast
to feel a real paging.The solution is based on one section to scroll paginated by items.
This is paginated by sections.
Another way is to create a custom UICollectionViewFlowLayout and override the method like so:
https://gist.github.com/mmick66/9812223
If you are looking for a Swift solution, I also created a little tutorial that includes code.
The solution Mike M. presented in the post before worked for me but in my case I wanted to have the first cell starting in the middle of the collectionView. So I used the collection flow delegate method to defined an inset (
collectionView:layout:insetForSectionAtIndex:
). This made the scroll between the first cell and second to be stuck and not scroll correctly to the first cell.The reason for this was that
candidateAttributes.center.x - halfWidth
was having a negative value. The solution was to get the absolute value so I add fabs to this linereturn CGPointMake(fabs(candidateAttributes.center.x - halfWidth), offset.y);
Fabs should be added by default to cover all situations.
You can snap to cells by being the delegate of the collection view and implementing the method:
This tells you that the user has finished a drag and it allows you to modify the
targetContentOffset
to align with your cells (i.e. round to the nearest cell). Note that you need to be careful about how you modify the targetContentOffset; in particular, you need to avoid changing it so that the view needs to scroll in the opposite direction of the passed velocity, or you'll get animation glitches. You can probably find many examples of this if you google for that method name.