Better cubic without affect layer of contentView

This commit is contained in:
WenchaoD 2017-03-01 14:26:04 +08:00
parent a4ee7171e5
commit 35691a6f78
4 changed files with 13 additions and 29 deletions

View File

@ -189,7 +189,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 originY = (self.collectionView!.frame.height-self.actualItemSize.height)
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)
return frame
@ -231,6 +231,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
return
}
let ruler = collectionView.bounds.midX
attributes.center.x = self.frame(for: attributes.indexPath).midX
attributes.position = (attributes.center.x-ruler)/self.itemSpan
attributes.interitemSpacing = self.actualInteritemSpacing
transformer.applyTransform(to: attributes)

View File

@ -174,9 +174,16 @@ open class FSPagerViewTransformer: NSObject {
attributes.transform = transform
attributes.zIndex = zIndex
case .cubic:
var pivot: CGPoint = CGPoint(x: 0.5, y: 0.5)
pivot.x = position < 0 ? 1 : 0
pivot.y = 0.5
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
@ -185,8 +192,6 @@ open class FSPagerViewTransformer: NSObject {
attributes.zIndex = 0
attributes.alpha = 0
}
attributes.pivot = pivot
attributes.rotationY = position * CGFloat(M_PI_2)
}
}

View File

@ -136,20 +136,4 @@ open class FSPagerViewCell: UICollectionViewCell {
}
}
open override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
let layoutAttributes = layoutAttributes as! FSPagerViewLayoutAttributes
self.contentView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.contentView.layer.transform = CATransform3DIdentity
guard layoutAttributes.rotationY != 0 else {
return
}
self.layer.transform = CATransform3DIdentity
var rotation = CATransform3DIdentity
rotation.m34 = -0.002
rotation = CATransform3DRotate(rotation, layoutAttributes.rotationY, 0, 1, 0)
self.contentView.layer.anchorPoint = layoutAttributes.pivot
self.contentView.layer.transform = rotation
}
}

View File

@ -9,9 +9,7 @@
import UIKit
open class FSPagerViewLayoutAttributes: UICollectionViewLayoutAttributes {
open var rotationY: CGFloat = 0
open var pivot = CGPoint(x:0.5, y:0.5)
open var position: CGFloat = 0
open var interitemSpacing: CGFloat = 0
@ -20,8 +18,6 @@ open class FSPagerViewLayoutAttributes: UICollectionViewLayoutAttributes {
return false
}
var isEqual = super.isEqual(object)
isEqual = isEqual && (self.rotationY == object.rotationY)
isEqual = isEqual && (self.pivot == object.pivot)
isEqual = isEqual && (self.position == object.position)
isEqual = isEqual && (self.interitemSpacing == object.interitemSpacing)
return isEqual
@ -29,8 +25,6 @@ open class FSPagerViewLayoutAttributes: UICollectionViewLayoutAttributes {
open override func copy(with zone: NSZone? = nil) -> Any {
let copy = super.copy(with: zone) as! FSPagerViewLayoutAttributes
copy.rotationY = self.rotationY
copy.pivot = self.pivot
copy.position = self.position
copy.interitemSpacing = self.interitemSpacing
return copy