Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customize the switch animation protocol #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@
TargetAttributes = {
F97C96761E1FDE25002D9E7E = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = HZF422TY46;
ProvisioningStyle = Automatic;
};
F9C694561E40C720007084B6 = {
Expand Down Expand Up @@ -467,7 +466,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = HZF422TY46;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/FSPagerViewExample/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSPagerViewExample;
Expand All @@ -480,7 +479,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = HZF422TY46;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/FSPagerViewExample/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.wenchaod.FSPagerViewExample;
Expand Down
6 changes: 3 additions & 3 deletions FSPagerView/FSPageViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class FSPagerViewLayout: UICollectionViewLayout {
}()

self.actualInteritemSpacing = {
if let transformer = pagerView.transformer {
return transformer.proposedInteritemSpacing()
if pagerView.transformer != nil {
return pagerView.itemSize.width*pagerView.transformer!.proposedInteritemSpacingPercent()
}
return pagerView.interitemSpacing
}()
Expand Down Expand Up @@ -239,7 +239,7 @@ class FSPagerViewLayout: UICollectionViewLayout {
}
let ruler = collectionView.bounds.midX
let position = (attributes.center.x-ruler)/self.itemSpan
transformer.applyTransform(to: attributes, for: position)
transformer.applyTransform(to: attributes, for: position, and: self.itemSpan)
}

}
Expand Down
31 changes: 12 additions & 19 deletions FSPagerView/FSPageViewTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public enum FSPagerViewTransformerType: Int {
case invertedFerrisWheel
}

open class FSPagerViewTransformer: NSObject {
open class FSPagerViewTransformer: NSObject, FSPagerViewTransitionAnimatior {

open internal(set) weak var pagerView: FSPagerView?
open internal(set) var type: FSPagerViewTransformerType = .none

open var minimumScale: CGFloat = 0.65
Expand All @@ -47,11 +46,10 @@ 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: UICollectionViewLayoutAttributes,
for position: CGFloat,
and itemSpan: CGFloat) {

switch self.type {
case .none:
break
Expand Down Expand Up @@ -156,7 +154,7 @@ open class FSPagerViewTransformer: NSObject {
var transform = CGAffineTransform.identity
switch position {
case -5 ... 5:
let itemSpacing = attributes.bounds.width+self.proposedInteritemSpacing()
let itemSpacing = attributes.bounds.width+attributes.size.width*self.proposedInteritemSpacingPercent()
let count: CGFloat = 14
let circle: CGFloat = CGFloat(M_PI) * 2.0
let radius = itemSpacing * count / circle
Expand All @@ -179,24 +177,19 @@ open class FSPagerViewTransformer: NSObject {
}

// An interitem spacing proposed by transformer class. This will override the default interitemSpacing provided by page slider.
open func proposedInteritemSpacing() -> CGFloat {
guard let pagerView = self.pagerView else {
return 0
}
open func proposedInteritemSpacingPercent() -> CGFloat {
switch self.type {
case .overlap:
return pagerView.itemSize.width * -self.minimumScale * 0.6
return -self.minimumScale * 0.6
case .linear:
return pagerView.itemSize.width * -self.minimumScale * 0.2
return -self.minimumScale * 0.2
case .coverFlow:
return -pagerView.itemSize.width * sin(CGFloat(M_PI_4)/4.0*3.0)
return sin(CGFloat(M_PI_4)/4.0*3.0)
case .ferrisWheel,.invertedFerrisWheel:
return -pagerView.itemSize.width * 0.15
return 0.15
default:
break
return 1
}
return pagerView.interitemSpacing
}

}

16 changes: 14 additions & 2 deletions FSPagerView/FSPagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

import UIKit

@objc
public protocol FSPagerViewTransitionAnimatior: NSObjectProtocol {
/// Customize your switching animation
@objc(applyTransformToAttributes:forPosition: itemSpan:)
func applyTransform(to attributes: UICollectionViewLayoutAttributes,
for position: CGFloat,
and itemSpan: CGFloat)

/// Get proposed interitem spacing percent
@objc
func proposedInteritemSpacingPercent() -> CGFloat
}

@objc
public protocol FSPagerViewDataSource: NSObjectProtocol {

Expand Down Expand Up @@ -137,9 +150,8 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
}

/// The transformer of the pager view
open var transformer: FSPagerViewTransformer? {
open var transformer: FSPagerViewTransitionAnimatior? {
didSet {
self.transformer?.pagerView = self
self.collectionViewLayout.forceInvalidate()
}
}
Expand Down