Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LeftPagingCellCollectionViewFlowLayout.swift
- // CollectionViewCustomLayout
- //
- // Created by Alexander Zimin on 17/03/2017.
- // Copyright © 2017 Alexander Zimin. All rights reserved.
- //
- import UIKit
- class LeftPagingCellCollectionViewFlowLayout: UICollectionViewFlowLayout {
- override func prepare() {
- super.prepare()
- collectionView?.decelerationRate = UIScrollViewDecelerationRateFast
- }
- private var recentOffset = CGPoint.zero
- var additionalMargin: CGFloat = 0
- override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
- guard let collectionView = collectionView else {
- return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
- }
- let cvBounds = CGRect(x: proposedContentOffset.x,
- y: proposedContentOffset.y,
- width: collectionView.bounds.width,
- height: collectionView.bounds.height)
- guard let attributesForVisibleCells = self.layoutAttributesForElements(in: cvBounds) else {
- return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
- }
- var candidateAttributes : UICollectionViewLayoutAttributes?
- for attributes in attributesForVisibleCells {
- if attributes.representedElementCategory != UICollectionElementCategory.cell {
- continue
- }
- if attributes.center.x < proposedContentOffset.x {
- continue
- }
- candidateAttributes = attributes
- break
- }
- if let candidateAttributes = candidateAttributes {
- var margin = self.additionalMargin
- if candidateAttributes.indexPath.row == 0 {
- margin = collectionView.contentInset.left
- }
- recentOffset = CGPoint(x: candidateAttributes.frame.origin.x - margin,
- y: proposedContentOffset.y)
- return recentOffset
- } else {
- return recentOffset
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement