Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.00 KB | None | 0 0
  1. //
  2. //  LeftPagingCellCollectionViewFlowLayout.swift
  3. //  CollectionViewCustomLayout
  4. //
  5. //  Created by Alexander Zimin on 17/03/2017.
  6. //  Copyright © 2017 Alexander Zimin. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class LeftPagingCellCollectionViewFlowLayout: UICollectionViewFlowLayout {
  12.  
  13.   override func prepare() {
  14.     super.prepare()
  15.     collectionView?.decelerationRate = UIScrollViewDecelerationRateFast
  16.   }
  17.  
  18.   private var recentOffset = CGPoint.zero
  19.   var additionalMargin: CGFloat = 0
  20.  
  21.   override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
  22.     guard let collectionView = collectionView else {
  23.       return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
  24.     }
  25.  
  26.     let cvBounds = CGRect(x: proposedContentOffset.x,
  27.                           y: proposedContentOffset.y,
  28.                           width: collectionView.bounds.width,
  29.                           height: collectionView.bounds.height)
  30.  
  31.     guard let attributesForVisibleCells = self.layoutAttributesForElements(in: cvBounds) else {
  32.       return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
  33.     }
  34.  
  35.     var candidateAttributes : UICollectionViewLayoutAttributes?
  36.     for attributes in attributesForVisibleCells {
  37.       if attributes.representedElementCategory != UICollectionElementCategory.cell {
  38.         continue
  39.       }
  40.  
  41.       if attributes.center.x < proposedContentOffset.x {
  42.         continue
  43.       }
  44.  
  45.       candidateAttributes = attributes
  46.       break
  47.     }
  48.  
  49.     if let candidateAttributes = candidateAttributes {
  50.       var margin = self.additionalMargin
  51.       if candidateAttributes.indexPath.row == 0 {
  52.         margin = collectionView.contentInset.left
  53.       }
  54.       recentOffset = CGPoint(x: candidateAttributes.frame.origin.x - margin,
  55.                              y: proposedContentOffset.y)
  56.       return recentOffset
  57.     } else {
  58.       return recentOffset
  59.     }
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement