Skip to content

Commit

Permalink
Add cubic transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
WenchaoD committed Feb 28, 2017
1 parent 61a7440 commit 3eccc8b
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ 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,
.linear,
.overlap,
.ferrisWheel,
.invertedFerrisWheel,
.coverFlow]
.coverFlow,
.cubic]
fileprivate var typeIndex = 0 {
didSet {
let type = self.transformerTypes[typeIndex]
Expand All @@ -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)
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion FSPagerView.podspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion FSPagerView/FSPagerView/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 3 additions & 4 deletions Sources/FSPageViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

}
Expand Down
39 changes: 28 additions & 11 deletions Sources/FSPageViewTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum FSPagerViewTransformerType: Int {
case coverFlow
case ferrisWheel
case invertedFerrisWheel
case cubic
}

open class FSPagerViewTransformer: NSObject {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
20 changes: 18 additions & 2 deletions Sources/FSPagerViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

}
26 changes: 22 additions & 4 deletions Sources/FSPagerViewLayoutAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


}

0 comments on commit 3eccc8b

Please sign in to comment.