Add cubic transformer
This commit is contained in:
parent
61a7440090
commit
3eccc8baac
|
|
@ -151,18 +151,10 @@
|
|||
F9C694281E40C583007084B6 /* Main.storyboard */,
|
||||
F9C694611E40C8DA007084B6 /* Resources */,
|
||||
F9C694371E40C59E007084B6 /* Supporting Files */,
|
||||
F9C6942D1E40C583007084B6 /* Resources */,
|
||||
);
|
||||
path = FSPagerViewExample;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
F9C6942D1E40C583007084B6 /* Resources */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
);
|
||||
path = Resources;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
F9C694371E40C59E007084B6 /* Supporting Files */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import UIKit
|
|||
class TransformerExampleViewController: UIViewController,FSPagerViewDataSource,FSPagerViewDelegate, UITableViewDataSource,UITableViewDelegate {
|
||||
|
||||
fileprivate let imageNames = ["1_1.jpg","1_2.jpg","1_3.jpg","1_4.jpg","1_5.jpg","1_6.jpg","1_7.jpg"]
|
||||
fileprivate let transformerNames = ["cross fading", "zoom out", "depth", "linear", "overlap", "ferris wheel", "inverted ferris wheel", "coverflow"]
|
||||
fileprivate let transformerNames = ["cross fading", "zoom out", "depth", "linear", "overlap", "ferris wheel", "inverted ferris wheel", "coverflow", "cubic"]
|
||||
fileprivate let transformerTypes: [FSPagerViewTransformerType] = [.crossFading,
|
||||
.zoomOut,
|
||||
.depth,
|
||||
|
|
@ -19,7 +19,8 @@ class TransformerExampleViewController: UIViewController,FSPagerViewDataSource,F
|
|||
.overlap,
|
||||
.ferrisWheel,
|
||||
.invertedFerrisWheel,
|
||||
.coverFlow]
|
||||
.coverFlow,
|
||||
.cubic]
|
||||
fileprivate var typeIndex = 0 {
|
||||
didSet {
|
||||
let type = self.transformerTypes[typeIndex]
|
||||
|
|
@ -34,8 +35,10 @@ class TransformerExampleViewController: UIViewController,FSPagerViewDataSource,F
|
|||
self.pagerView.itemSize = CGSize(width: 180, height: 140)
|
||||
case .coverFlow:
|
||||
self.pagerView.itemSize = CGSize(width: 220, height: 170)
|
||||
case .cubic:
|
||||
let transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
|
||||
self.pagerView.itemSize = self.pagerView.frame.size.applying(transform)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
Pod::Spec.new do |s|
|
||||
|
||||
s.name = "FSPagerView"
|
||||
s.version = "0.2.1"
|
||||
s.version = "0.3.0"
|
||||
s.summary = "FSPagerView is an elegant Screen Slide Library for making Banner、Product Show、Welcome/Guide Pages、Screen/ViewController Sliders."
|
||||
|
||||
s.homepage = "https://github.com/WenchaoD/FSPagerView"
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.2.1</string>
|
||||
<string>0.3.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
|
|
|
|||
|
|
@ -141,8 +141,6 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
return attributes
|
||||
}
|
||||
let attributes = FSPagerViewLayoutAttributes(forCellWith: indexPath)
|
||||
attributes.itemSize = self.actualItemSize
|
||||
attributes.interitemSpacing = self.actualInteritemSpacing
|
||||
let x = self.frame(for: indexPath).minX
|
||||
let center = CGPoint(x: x+self.actualItemSize.width*0.5, y: collectionView.frame.height*0.5)
|
||||
attributes.center = center
|
||||
|
|
@ -233,8 +231,9 @@ class FSPagerViewLayout: UICollectionViewLayout {
|
|||
return
|
||||
}
|
||||
let ruler = collectionView.bounds.midX
|
||||
let position = (attributes.center.x-ruler)/self.itemSpan
|
||||
transformer.applyTransform(to: attributes, for: position)
|
||||
attributes.position = (attributes.center.x-ruler)/self.itemSpan
|
||||
attributes.interitemSpacing = self.actualInteritemSpacing
|
||||
transformer.applyTransform(to: attributes)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ public enum FSPagerViewTransformerType: Int {
|
|||
case coverFlow
|
||||
case ferrisWheel
|
||||
case invertedFerrisWheel
|
||||
case cubic
|
||||
}
|
||||
|
||||
open class FSPagerViewTransformer: NSObject {
|
||||
|
|
@ -47,15 +48,16 @@ open class FSPagerViewTransformer: NSObject {
|
|||
}
|
||||
|
||||
// Apply transform to attributes - zIndex: Int, frame: CGRect, alpha: CGFloat, transform: CGAffineTransform or transform3D: CATransform3D.
|
||||
open func applyTransform(to attributes: FSPagerViewLayoutAttributes, for position: CGFloat) {
|
||||
let itemSpan = attributes.interitemSpacing + attributes.itemSize.width
|
||||
open func applyTransform(to attributes: FSPagerViewLayoutAttributes) {
|
||||
let position = attributes.position
|
||||
let itemSpacing = attributes.interitemSpacing + attributes.bounds.width
|
||||
switch self.type {
|
||||
case .none:
|
||||
break
|
||||
case .crossFading:
|
||||
var zIndex = 0
|
||||
var alpha: CGFloat = 0
|
||||
let transform = CGAffineTransform(translationX: -itemSpan * position, y: 0)
|
||||
let transform = CGAffineTransform(translationX: -itemSpacing * position, y: 0)
|
||||
if (abs(position) < 1) { // [-1,1]
|
||||
// Use the default slide transition when moving to the left page
|
||||
alpha = 1 - abs(position)
|
||||
|
|
@ -79,7 +81,7 @@ open class FSPagerViewTransformer: NSObject {
|
|||
// Modify the default slide transition to shrink the page as well
|
||||
let scaleFactor = max(self.minimumScale, 1 - abs(position))
|
||||
let vertMargin = attributes.bounds.height * (1 - scaleFactor) / 2;
|
||||
let horzMargin = itemSpan * (1 - scaleFactor) / 2;
|
||||
let horzMargin = itemSpacing * (1 - scaleFactor) / 2;
|
||||
transform.a = scaleFactor
|
||||
transform.d = scaleFactor
|
||||
transform.tx = position < 0 ? (horzMargin - vertMargin*2) : (-horzMargin + vertMargin*2)
|
||||
|
|
@ -113,7 +115,7 @@ open class FSPagerViewTransformer: NSObject {
|
|||
// Fade the page out.
|
||||
alpha = CGFloat(1.0) - position
|
||||
// Counteract the default slide transition
|
||||
transform.tx = itemSpan * -position;
|
||||
transform.tx = itemSpacing * -position;
|
||||
// Scale the page down (between minimumScale and 1)
|
||||
let scaleFactor = self.minimumScale
|
||||
+ (1.0 - self.minimumScale) * (1.0 - abs(position));
|
||||
|
|
@ -141,14 +143,15 @@ open class FSPagerViewTransformer: NSObject {
|
|||
case .coverFlow:
|
||||
let position = min(max(-position,-1) ,1)
|
||||
let rotation = sin(position*CGFloat(M_PI_2)) * CGFloat(M_PI_4)*1.5
|
||||
let translationZ = -itemSpan * 0.5*abs(position)
|
||||
let translationZ = -itemSpacing * 0.5 * abs(position)
|
||||
var transform3D = CATransform3DIdentity
|
||||
transform3D.m34 = -0.002
|
||||
transform3D = CATransform3DRotate(transform3D,rotation, 0, 1, 0)
|
||||
transform3D = CATransform3DTranslate(transform3D,0, 0, translationZ)
|
||||
transform3D = CATransform3DRotate(transform3D, rotation, 0, 1, 0)
|
||||
transform3D = CATransform3DTranslate(transform3D, 0, 0, translationZ)
|
||||
attributes.zIndex = 100 - Int(abs(position))
|
||||
attributes.transform3D = transform3D
|
||||
case .ferrisWheel, .invertedFerrisWheel:
|
||||
// http://ronnqvi.st/translate-rotate-translate/
|
||||
var zIndex = 0
|
||||
var transform = CGAffineTransform.identity
|
||||
switch position {
|
||||
|
|
@ -164,18 +167,30 @@ open class FSPagerViewTransformer: NSObject {
|
|||
transform = transform.rotated(by: rotation)
|
||||
transform = transform.translatedBy(x: 0, y: -ty)
|
||||
zIndex = Int((4.0-abs(position)*10))
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
attributes.alpha = abs(position) < 0.5 ? 1 : self.minimumAlpha
|
||||
attributes.transform = transform
|
||||
attributes.zIndex = zIndex
|
||||
break
|
||||
case .cubic:
|
||||
var pivot: CGPoint = CGPoint(x: 0.5, y: 0.5)
|
||||
pivot.x = position < 0 ? 1 : 0
|
||||
pivot.y = 0.5
|
||||
switch position {
|
||||
case -1...1:
|
||||
attributes.alpha = 1
|
||||
attributes.zIndex = Int((1-position) * CGFloat(10))
|
||||
default:
|
||||
attributes.zIndex = 0
|
||||
attributes.alpha = 0
|
||||
}
|
||||
attributes.pivot = pivot
|
||||
attributes.rotationY = position * CGFloat(M_PI_2)
|
||||
}
|
||||
}
|
||||
|
||||
// An interitem spacing proposed by transformer class. This will override the default interitemSpacing provided by page slider.
|
||||
// An interitem spacing proposed by transformer class. This will override the default interitemSpacing provided by the pager view.
|
||||
open func proposedInteritemSpacing() -> CGFloat {
|
||||
guard let pagerView = self.pagerView else {
|
||||
return 0
|
||||
|
|
@ -189,6 +204,8 @@ open class FSPagerViewTransformer: NSObject {
|
|||
return -pagerView.itemSize.width * sin(CGFloat(M_PI_4)/4.0*3.0)
|
||||
case .ferrisWheel,.invertedFerrisWheel:
|
||||
return -pagerView.itemSize.width * 0.15
|
||||
case .cubic:
|
||||
return 0
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,12 +86,12 @@ open class FSPagerViewCell: UICollectionViewCell {
|
|||
}
|
||||
|
||||
fileprivate func commonInit() {
|
||||
self.contentView.backgroundColor = UIColor.white
|
||||
self.contentView.backgroundColor = UIColor.clear
|
||||
self.backgroundColor = UIColor.clear
|
||||
self.contentView.layer.shadowColor = UIColor.black.cgColor
|
||||
self.contentView.layer.shadowRadius = 5
|
||||
self.contentView.layer.shadowOpacity = 0.75
|
||||
self.contentView.layer.shadowOffset = .zero
|
||||
self.backgroundColor = UIColor.white
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
|
@ -136,4 +136,20 @@ 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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,12 +10,30 @@ import UIKit
|
|||
|
||||
open class FSPagerViewLayoutAttributes: UICollectionViewLayoutAttributes {
|
||||
|
||||
open var itemSize: CGSize = .zero
|
||||
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
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
open override func isEqual(_ object: Any?) -> Bool {
|
||||
guard let object = object as? FSPagerViewLayoutAttributes else {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue