Remove unnecessary calculation
This commit is contained in:
parent
35691a6f78
commit
e4ed35a162
|
|
@ -12,7 +12,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
|
||||
internal var contentSize: CGSize = .zero
|
||||
internal var leadingSpacing: CGFloat = 0
|
||||
internal var itemSpan: CGFloat = 0
|
||||
internal var itemSpacing: CGFloat = 0
|
||||
internal var needsReprepare = true
|
||||
|
||||
open override class var layoutAttributesClass: AnyClass {
|
||||
|
|
@ -83,7 +83,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
return pagerView.interitemSpacing
|
||||
}()
|
||||
self.leadingSpacing = (collectionView.frame.width-self.actualItemSize.width)*0.5
|
||||
self.itemSpan = self.actualItemSize.width+self.actualInteritemSpacing
|
||||
self.itemSpacing = self.actualItemSize.width+self.actualInteritemSpacing
|
||||
|
||||
// Calculate and cache contentSize, rather than calculating each time
|
||||
self.contentSize = {
|
||||
|
|
@ -107,7 +107,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
|
||||
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
|
||||
var layoutAttributes = [UICollectionViewLayoutAttributes]()
|
||||
guard self.itemSpan > 0, !rect.isEmpty else {
|
||||
guard self.itemSpacing > 0, !rect.isEmpty else {
|
||||
return layoutAttributes
|
||||
}
|
||||
let rect = rect.intersection(CGRect(origin: .zero, size: self.contentSize))
|
||||
|
|
@ -115,8 +115,8 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
return layoutAttributes
|
||||
}
|
||||
// Calculate start position and index of certain rects
|
||||
let numberOfItemsBefore = max(Int((rect.minX-self.leadingSpacing)/self.itemSpan),0)
|
||||
let startPosition = self.leadingSpacing + CGFloat(numberOfItemsBefore)*self.itemSpan
|
||||
let numberOfItemsBefore = max(Int((rect.minX-self.leadingSpacing)/self.itemSpacing),0)
|
||||
let startPosition = self.leadingSpacing + CGFloat(numberOfItemsBefore)*self.itemSpacing
|
||||
let startIndex = numberOfItemsBefore
|
||||
// Create layout attributes
|
||||
var itemIndex = startIndex
|
||||
|
|
@ -128,7 +128,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
self.applyTransform(to: attributes)
|
||||
layoutAttributes.append(attributes)
|
||||
itemIndex += 1
|
||||
x += self.itemSpan
|
||||
x += self.itemSpacing
|
||||
}
|
||||
return layoutAttributes
|
||||
}
|
||||
|
|
@ -156,12 +156,12 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
var proposedContentOffset = proposedContentOffset
|
||||
let proposedContentOffsetX: CGFloat = {
|
||||
let translation = -collectionView.panGestureRecognizer.translation(in: collectionView).x
|
||||
var offset: CGFloat = round(proposedContentOffset.x/self.itemSpan)*self.itemSpan
|
||||
let minFlippingDistance = min(0.5 * self.itemSpan,150)
|
||||
var offset: CGFloat = round(proposedContentOffset.x/self.itemSpacing)*self.itemSpacing
|
||||
let minFlippingDistance = min(0.5 * self.itemSpacing,150)
|
||||
let originalContentOffsetX = collectionView.contentOffset.x - translation
|
||||
if abs(translation) <= minFlippingDistance {
|
||||
if abs(velocity.x) >= 0.3 && abs(proposedContentOffset.x-originalContentOffsetX) <= self.itemSpan*0.5 {
|
||||
offset += self.itemSpan * (velocity.x)/abs(velocity.x)
|
||||
if abs(velocity.x) >= 0.3 && abs(proposedContentOffset.x-originalContentOffsetX) <= self.itemSpacing*0.5 {
|
||||
offset += self.itemSpacing * (velocity.x)/abs(velocity.x)
|
||||
}
|
||||
}
|
||||
return offset
|
||||
|
|
@ -188,7 +188,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
|
||||
internal func frame(for indexPath: IndexPath) -> CGRect {
|
||||
let numberOfItems = self.numberOfItems*indexPath.section + indexPath.item
|
||||
let originX = self.leadingSpacing + CGFloat(numberOfItems)*self.itemSpan
|
||||
let originX = self.leadingSpacing + CGFloat(numberOfItems)*self.itemSpacing
|
||||
let originY = (self.collectionView!.frame.height-self.actualItemSize.height)/2.0
|
||||
let origin = CGPoint(x: originX, y: originY)
|
||||
let frame = CGRect(origin: origin, size: self.actualItemSize)
|
||||
|
|
@ -232,7 +232,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
}
|
||||
let ruler = collectionView.bounds.midX
|
||||
attributes.center.x = self.frame(for: attributes.indexPath).midX
|
||||
attributes.position = (attributes.center.x-ruler)/self.itemSpan
|
||||
attributes.position = (attributes.center.x-ruler)/self.itemSpacing
|
||||
attributes.interitemSpacing = self.actualInteritemSpacing
|
||||
transformer.applyTransform(to: attributes)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -174,20 +174,20 @@ open class FSPagerViewTransformer: NSObject {
|
|||
attributes.transform = transform
|
||||
attributes.zIndex = zIndex
|
||||
case .cubic:
|
||||
let direction: CGFloat = position < 0 ? 1 : -1
|
||||
let theta = position * CGFloat(M_PI_2)
|
||||
let width = attributes.bounds.width
|
||||
// ForwardX -> RotateY -> BackwardX
|
||||
attributes.center.x += direction*width*0.5 // ForwardX
|
||||
var transform3D = CATransform3DIdentity
|
||||
transform3D.m34 = -0.002
|
||||
transform3D = CATransform3DRotate(transform3D, theta, 0, 1, 0) // RotateY
|
||||
transform3D = CATransform3DTranslate(transform3D,-direction*width*0.5, 0, 0) // BackwardX
|
||||
attributes.transform3D = transform3D
|
||||
switch position {
|
||||
case -1...1:
|
||||
attributes.alpha = 1
|
||||
attributes.zIndex = Int((1-position) * CGFloat(10))
|
||||
let direction: CGFloat = position < 0 ? 1 : -1
|
||||
let theta = position * CGFloat(M_PI_2)
|
||||
let width = attributes.bounds.width
|
||||
// ForwardX -> RotateY -> BackwardX
|
||||
attributes.center.x += direction*width*0.5 // ForwardX
|
||||
var transform3D = CATransform3DIdentity
|
||||
transform3D.m34 = -0.002
|
||||
transform3D = CATransform3DRotate(transform3D, theta, 0, 1, 0) // RotateY
|
||||
transform3D = CATransform3DTranslate(transform3D,-direction*width*0.5, 0, 0) // BackwardX
|
||||
attributes.transform3D = transform3D
|
||||
default:
|
||||
attributes.zIndex = 0
|
||||
attributes.alpha = 0
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
|
|||
|
||||
/// The percentage of x position at which the origin of the content view is offset from the origin of the pagerView view.
|
||||
open var scrollOffset: CGFloat {
|
||||
let scrollOffset = Double(self.collectionView.contentOffset.x.divided(by: self.collectionViewLayout.itemSpan))
|
||||
let scrollOffset = Double(self.collectionView.contentOffset.x.divided(by: self.collectionViewLayout.itemSpacing))
|
||||
return fmod(CGFloat(scrollOffset), CGFloat(Double(self.numberOfItems)))
|
||||
}
|
||||
|
||||
|
|
@ -322,7 +322,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
|
|||
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
||||
if self.numberOfItems > 0 {
|
||||
// In case someone is using KVO
|
||||
let currentIndex = lround(Double(self.scrollOffset))
|
||||
let currentIndex = lround(Double(self.scrollOffset)) % self.numberOfItems
|
||||
if (currentIndex != self.currentIndex) {
|
||||
self.currentIndex = currentIndex
|
||||
}
|
||||
|
|
@ -344,7 +344,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
|
|||
|
||||
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
|
||||
if let function = self.delegate?.pagerViewWillEndDragging(_:targetIndex:) {
|
||||
let targetItem = lround(Double(targetContentOffset.pointee.x/self.collectionViewLayout.itemSpan))
|
||||
let targetItem = lround(Double(targetContentOffset.pointee.x/self.collectionViewLayout.itemSpacing))
|
||||
function(self, targetItem % self.numberOfItems)
|
||||
}
|
||||
if self.automaticSlidingInterval > 0 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue