Skip to content

Commit

Permalink
Better cubic without affect layer of contentView
Browse files Browse the repository at this point in the history
  • Loading branch information
WenchaoD committed Mar 1, 2017
1 parent a4ee717 commit 35691a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Sources/FSPageViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions Sources/FSPageViewTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -185,8 +192,6 @@ open class FSPagerViewTransformer: NSObject {
attributes.zIndex = 0
attributes.alpha = 0
}
attributes.pivot = pivot
attributes.rotationY = position * CGFloat(M_PI_2)
}
}

Expand Down
16 changes: 0 additions & 16 deletions Sources/FSPagerViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
8 changes: 1 addition & 7 deletions Sources/FSPagerViewLayoutAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,17 +18,13 @@ 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
}

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
Expand Down

0 comments on commit 35691a6

Please sign in to comment.