Skip to content

Instantly share code, notes, and snippets.

@AntonTheDev
Last active May 22, 2020 21:27
Show Gist options
  • Select an option

  • Save AntonTheDev/b2822a7df651fdefaeacbe5a392bb975 to your computer and use it in GitHub Desktop.

Select an option

Save AntonTheDev/b2822a7df651fdefaeacbe5a392bb975 to your computer and use it in GitHub Desktop.
Slow Easing CollectionView and FlowLayout
import Foundation
import UIKit
class UIDirectionAbidingSlowingCollectionView : _UIDirectionAbidingCollectionView
{
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
{
super.init(frame: frame, collectionViewLayout: layout)
let decelerationRate : CGFloat = 0.996
setValue(NSValue(cgPoint: CGPoint(x: decelerationRate, y: decelerationRate)), forKey: "_" + "decelerationFactor")
alwaysBounceVertical = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
public class BounceProofCollectionViewFlowLayout : UICollectionViewFlowLayout
{
var lastVelocity : CGPoint = CGPoint.zero
var enableEndBounce : Bool = false
var invalidateOnScroll : Bool = false
var lastProposedOffset : CGPoint = CGPoint.zero
public override func invalidateLayout() {
super.invalidateLayout()
// print("BounceProofCollectionViewFlowLayout Invalidated Layout ")
}
override init()
{
super.init()
self.minimumInteritemSpacing = 0.0
self.minimumLineSpacing = 0.0
self.sectionHeadersPinToVisibleBounds = true
self.minimumInteritemSpacing = 0.0
self.minimumLineSpacing = 0.0
self.minimumInteritemSpacing = 0
self.minimumLineSpacing = 0
self.sectionInset = UIEdgeInsets.zero
self.scrollDirection = .vertical
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool
{
return invalidateOnScroll
}
override public func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
lastVelocity = velocity
if scrollDirection == .vertical
{
var _proposedContentOffset = proposedContentOffset
if _proposedContentOffset.y < (1.0 - (self.collectionView?.contentInset.top ?? CGFloat(0.0)))
{
_proposedContentOffset.y = 1.0 - (self.collectionView?.contentInset.top ?? CGFloat(0.0))
}
else if _proposedContentOffset.y >= self.collectionView!.contentSize.height - self.collectionView!.bounds.height + self.collectionView!.contentInset.bottom, enableEndBounce == false
{
_proposedContentOffset.y = self.collectionView!.contentSize.height - self.collectionView!.bounds.height + self.collectionView!.contentInset.bottom - 1
}
lastProposedOffset = _proposedContentOffset
return _proposedContentOffset
}
var _proposedContentOffset = proposedContentOffset
if _proposedContentOffset.x < (1.0 - (self.collectionView?.contentInset.left ?? CGFloat(0.0)))
{
_proposedContentOffset.x = 1.0 - (self.collectionView?.contentInset.left ?? CGFloat(0.0))
}
else if _proposedContentOffset.x >= self.collectionView!.contentSize.width - self.collectionView!.bounds.width + self.collectionView!.contentInset.right, enableEndBounce == false
{
_proposedContentOffset.x = self.collectionView!.contentSize.width - self.collectionView!.bounds.width + self.collectionView!.contentInset.right - 1 - self.collectionView!.contentInset.left
}
lastProposedOffset = _proposedContentOffset
return _proposedContentOffset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment