diff --git a/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj b/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj index 80af4f5..3958450 100644 --- a/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj +++ b/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj @@ -151,18 +151,10 @@ F9C694281E40C583007084B6 /* Main.storyboard */, F9C694611E40C8DA007084B6 /* Resources */, F9C694371E40C59E007084B6 /* Supporting Files */, - F9C6942D1E40C583007084B6 /* Resources */, ); path = FSPagerViewExample; sourceTree = ""; }; - F9C6942D1E40C583007084B6 /* Resources */ = { - isa = PBXGroup; - children = ( - ); - path = Resources; - sourceTree = ""; - }; F9C694371E40C59E007084B6 /* Supporting Files */ = { isa = PBXGroup; children = ( diff --git a/FSPageViewExample-Swift/FSPagerViewExample/TransformerExampleViewController.swift b/FSPageViewExample-Swift/FSPagerViewExample/TransformerExampleViewController.swift index 5c05624..e5b3af9 100644 --- a/FSPageViewExample-Swift/FSPagerViewExample/TransformerExampleViewController.swift +++ b/FSPageViewExample-Swift/FSPagerViewExample/TransformerExampleViewController.swift @@ -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) } - } } diff --git a/FSPagerView.podspec b/FSPagerView.podspec index b006778..2087852 100644 --- a/FSPagerView.podspec +++ b/FSPagerView.podspec @@ -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" diff --git a/FSPagerView/FSPagerView/Info.plist b/FSPagerView/FSPagerView/Info.plist index 600e701..0d6341f 100644 --- a/FSPagerView/FSPagerView/Info.plist +++ b/FSPagerView/FSPagerView/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.2.1 + 0.3.0 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/Sources/FSPageViewLayout.swift b/Sources/FSPageViewLayout.swift index cbd0aa1..cf3bb19 100644 --- a/Sources/FSPageViewLayout.swift +++ b/Sources/FSPageViewLayout.swift @@ -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) } } diff --git a/Sources/FSPageViewTransformer.swift b/Sources/FSPageViewTransformer.swift index 58fb57b..6c54388 100644 --- a/Sources/FSPageViewTransformer.swift +++ b/Sources/FSPageViewTransformer.swift @@ -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 } diff --git a/Sources/FSPagerViewCell.swift b/Sources/FSPagerViewCell.swift index 93168b1..69393ac 100644 --- a/Sources/FSPagerViewCell.swift +++ b/Sources/FSPagerViewCell.swift @@ -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 + } + } diff --git a/Sources/FSPagerViewLayoutAttributes.swift b/Sources/FSPagerViewLayoutAttributes.swift index 4e11ea3..7d6f6ef 100644 --- a/Sources/FSPagerViewLayoutAttributes.swift +++ b/Sources/FSPagerViewLayoutAttributes.swift @@ -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 } - }