diff --git a/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj b/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj index 23a81bc..80af4f5 100644 --- a/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj +++ b/FSPageViewExample-Swift/FSPagerViewExample.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + F954839A1E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95483991E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift */; }; + F954839B1E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95483991E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift */; }; F9580B571E5D995400C5B267 /* FSPageControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9580B511E5D995400C5B267 /* FSPageControl.swift */; }; F9580B581E5D995400C5B267 /* FSPagerCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9580B521E5D995400C5B267 /* FSPagerCollectionView.swift */; }; F9580B591E5D995400C5B267 /* FSPagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9580B531E5D995400C5B267 /* FSPagerView.swift */; }; @@ -49,6 +51,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + F95483991E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FSPagerViewLayoutAttributes.swift; sourceTree = ""; }; F9580B511E5D995400C5B267 /* FSPageControl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FSPageControl.swift; sourceTree = ""; }; F9580B521E5D995400C5B267 /* FSPagerCollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FSPagerCollectionView.swift; sourceTree = ""; }; F9580B531E5D995400C5B267 /* FSPagerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FSPagerView.swift; sourceTree = ""; }; @@ -111,6 +114,7 @@ F9580B541E5D995400C5B267 /* FSPagerViewCell.swift */, F9580B551E5D995400C5B267 /* FSPageViewLayout.swift */, F9580B561E5D995400C5B267 /* FSPageViewTransformer.swift */, + F95483991E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift */, ); name = Sources; path = ../Sources; @@ -322,6 +326,7 @@ F9580B571E5D995400C5B267 /* FSPageControl.swift in Sources */, F9580B581E5D995400C5B267 /* FSPagerCollectionView.swift in Sources */, F9C6942F1E40C583007084B6 /* AppDelegate.swift in Sources */, + F954839A1E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift in Sources */, F9580B591E5D995400C5B267 /* FSPagerView.swift in Sources */, F9C694351E40C583007084B6 /* PageControlExampleViewController.swift in Sources */, F9C694361E40C583007084B6 /* TransformerExampleViewController.swift in Sources */, @@ -336,6 +341,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + F954839B1E625F1E0069FD7E /* FSPagerViewLayoutAttributes.swift in Sources */, F9C6945A1E40C720007084B6 /* FSPagerViewExampleUITests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/FSPageViewExample-Swift/FSPagerViewExample/Base.lproj/Main.storyboard b/FSPageViewExample-Swift/FSPagerViewExample/Base.lproj/Main.storyboard index dd6604a..1202356 100644 --- a/FSPageViewExample-Swift/FSPagerViewExample/Base.lproj/Main.storyboard +++ b/FSPageViewExample-Swift/FSPagerViewExample/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -234,6 +234,9 @@ + + + diff --git a/Sources/FSPageViewLayout.swift b/Sources/FSPageViewLayout.swift index 8993ba4..84da1b1 100644 --- a/Sources/FSPageViewLayout.swift +++ b/Sources/FSPageViewLayout.swift @@ -15,10 +15,16 @@ class FSPagerViewLayout: UICollectionViewLayout { internal var itemSpan: CGFloat = 0 internal var needsReprepare = true + open override class var layoutAttributesClass: AnyClass { + get { + return FSPagerViewLayoutAttributes.self + } + } + fileprivate var pagerView: FSPagerView? { return self.collectionView?.superview?.superview as? FSPagerView } - fileprivate var layoutAttributes: [IndexPath:UICollectionViewLayoutAttributes] = [:] + fileprivate var layoutAttributes: [IndexPath:FSPagerViewLayoutAttributes] = [:] fileprivate var isInfinite: Bool = true fileprivate var collectionViewSize: CGSize = .zero @@ -118,7 +124,7 @@ class FSPagerViewLayout: UICollectionViewLayout { let maxPosition = min(rect.maxX,self.contentSize.width-self.actualItemSize.width) while x <= maxPosition { let indexPath = IndexPath(item: itemIndex%self.numberOfItems, section: itemIndex/self.numberOfItems) - let attributes = self.layoutAttributesForItem(at: indexPath)! + let attributes = self.layoutAttributesForItem(at: indexPath) as! FSPagerViewLayoutAttributes self.applyTransform(to: attributes) layoutAttributes.append(attributes) itemIndex += 1 @@ -134,7 +140,9 @@ class FSPagerViewLayout: UICollectionViewLayout { if let attributes = self.layoutAttributes[indexPath] { return attributes } - let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath) + 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 @@ -217,7 +225,7 @@ class FSPagerViewLayout: UICollectionViewLayout { collectionView.bounds = newBounds } - fileprivate func applyTransform(to attributes: UICollectionViewLayoutAttributes) { + fileprivate func applyTransform(to attributes: FSPagerViewLayoutAttributes) { guard let collectionView = self.collectionView else { return } diff --git a/Sources/FSPageViewTransformer.swift b/Sources/FSPageViewTransformer.swift index e52df69..58fb57b 100644 --- a/Sources/FSPageViewTransformer.swift +++ b/Sources/FSPageViewTransformer.swift @@ -47,11 +47,8 @@ open class FSPagerViewTransformer: NSObject { } // Apply transform to attributes - zIndex: Int, frame: CGRect, alpha: CGFloat, transform: CGAffineTransform or transform3D: CATransform3D. - open func applyTransform(to attributes: UICollectionViewLayoutAttributes, for position: CGFloat) { - guard let pagerView = self.pagerView else { - return - } - let itemSpan = pagerView.collectionViewLayout.itemSpan + open func applyTransform(to attributes: FSPagerViewLayoutAttributes, for position: CGFloat) { + let itemSpan = attributes.interitemSpacing + attributes.itemSize.width switch self.type { case .none: break diff --git a/Sources/FSPagerViewLayoutAttributes.swift b/Sources/FSPagerViewLayoutAttributes.swift new file mode 100644 index 0000000..4e11ea3 --- /dev/null +++ b/Sources/FSPagerViewLayoutAttributes.swift @@ -0,0 +1,21 @@ +// +// FSPagerViewLayoutAttributes.swift +// FSPagerViewExample +// +// Created by Wenchao Ding on 26/02/2017. +// Copyright © 2017 Wenchao Ding. All rights reserved. +// + +import UIKit + +open class FSPagerViewLayoutAttributes: UICollectionViewLayoutAttributes { + + open var itemSize: CGSize = .zero + open var interitemSpacing: CGFloat = 0 + + override init() { + super.init() + } + + +}