From 8bea613d35423046b9b12dffc0b60940ed2fd665 Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 11 Feb 2017 14:53:13 -0500 Subject: [PATCH] project cleanup --- .swiftlint.yml | 5 ++ .../HeroExamples.xcodeproj/project.pbxproj | 14 +++++ .../AnimationSelectTableViewController.swift | 1 - Sources/CG+Hero.swift | 13 +++-- Sources/CascadePreprocessor.swift | 8 +-- Sources/Hero.swift | 49 +++++++---------- Sources/HeroBaseController.swift | 4 +- Sources/HeroContext.swift | 28 +++++----- Sources/HeroDebugView.swift | 8 +-- Sources/HeroDefaultAnimator.swift | 4 +- Sources/HeroDefaultAnimatorViewContext.swift | 12 ++--- Sources/HeroIndependentController.swift | 2 +- Sources/HeroModifier.swift | 10 ++-- Sources/HeroPlugin.swift | 12 ++--- Sources/HeroTargetState.swift | 16 +++--- Sources/HeroTypes.swift | 8 +-- .../IgnoreSubviewModifiersPreprocessor.swift | 2 +- Sources/SourcePreprocessor.swift | 4 +- Sources/UIKit+Hero.swift | 53 +++++++++---------- 19 files changed, 126 insertions(+), 127 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 1b0c5ef2..fdbc288e 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -2,6 +2,11 @@ disabled_rules: # rule identifiers to exclude from running - missing_docs - unused_closure_parameter - force_cast + - weak_delegate + - variable_name + - cyclomatic_complexity + - function_body_length + - todo opt_in_rules: # some rules are only opt-in - empty_count diff --git a/Examples/HeroExamples.xcodeproj/project.pbxproj b/Examples/HeroExamples.xcodeproj/project.pbxproj index 846a727f..a60ce8ee 100644 --- a/Examples/HeroExamples.xcodeproj/project.pbxproj +++ b/Examples/HeroExamples.xcodeproj/project.pbxproj @@ -550,6 +550,7 @@ isa = PBXNativeTarget; buildConfigurationList = A306D3BE1E1C7A2E00B6C23A /* Build configuration list for PBXNativeTarget "Hero (iOS)" */; buildPhases = ( + B16ECD011E4FA11500EAE0E0 /* ShellScript */, A306D3AD1E1C7A2E00B6C23A /* Sources */, A306D3AE1E1C7A2E00B6C23A /* Frameworks */, A306D3AF1E1C7A2E00B6C23A /* Headers */, @@ -749,6 +750,19 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; + B16ECD011E4FA11500EAE0E0 /* ShellScript */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi"; + }; B60F2CF0B495F2D785FF29A1 /* [CP] Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; diff --git a/Examples/HeroExamples/Examples/DefaultAnimation/AnimationSelectTableViewController.swift b/Examples/HeroExamples/Examples/DefaultAnimation/AnimationSelectTableViewController.swift index f0ef7544..38e506d2 100644 --- a/Examples/HeroExamples/Examples/DefaultAnimation/AnimationSelectTableViewController.swift +++ b/Examples/HeroExamples/Examples/DefaultAnimation/AnimationSelectTableViewController.swift @@ -53,7 +53,6 @@ class AnimationSelectTableViewController: UITableViewController { Hero.shared.setDefaultAnimationForNextTransition(animations[indexPath.item]) Hero.shared.setContainerColorForNextTransition(.lightGray) - hero_replaceViewController(with: vc) } diff --git a/Sources/CG+Hero.swift b/Sources/CG+Hero.swift index 5b119451..45c3cc61 100644 --- a/Sources/CG+Hero.swift +++ b/Sources/CG+Hero.swift @@ -100,16 +100,16 @@ internal func - (left: CGPoint, right: CGPoint) -> CGPoint { internal func / (left: CGPoint, right: CGFloat) -> CGPoint { return CGPoint(x: left.x/right, y: left.y/right) } -internal func /(left: CGPoint, right: CGPoint) -> CGPoint { +internal func / (left: CGPoint, right: CGPoint) -> CGPoint { return CGPoint(x: left.x/right.x, y: left.y/right.y) } -internal func *(left: CGPoint, right: CGFloat) -> CGPoint { +internal func * (left: CGPoint, right: CGFloat) -> CGPoint { return CGPoint(x: left.x*right, y: left.y*right) } -internal func *(left: CGPoint, right: CGSize) -> CGPoint { +internal func * (left: CGPoint, right: CGSize) -> CGPoint { return CGPoint(x: left.x*right.width, y: left.y*right.width) } -internal func *(left: CGFloat, right: CGPoint) -> CGPoint { +internal func * (left: CGFloat, right: CGPoint) -> CGPoint { return right * left } @@ -128,14 +128,13 @@ internal func abs(_ p: CGPoint) -> CGPoint { internal func * (left: CGSize, right: CGFloat) -> CGSize { return CGSize(width: left.width*right, height: left.height*right) } -internal func *(left: CGSize, right: CGSize) -> CGSize { +internal func * (left: CGSize, right: CGSize) -> CGSize { return CGSize(width: left.width*right.width, height: left.height*right.width) } -internal func /(left: CGSize, right: CGSize) -> CGSize { +internal func / (left: CGSize, right: CGSize) -> CGSize { return CGSize(width: left.width/right.width, height: left.height/right.height) } - internal func == (lhs: CATransform3D, rhs: CATransform3D) -> Bool { var lhs = lhs var rhs = rhs diff --git a/Sources/CascadePreprocessor.swift b/Sources/CascadePreprocessor.swift index f47b8229..bec75b09 100644 --- a/Sources/CascadePreprocessor.swift +++ b/Sources/CascadePreprocessor.swift @@ -47,7 +47,7 @@ public enum CascadeDirection { return { return $0.center.distance(center) > $1.center.distance(center) } } } - + init?(_ string: String) { switch string { case "bottomToTop": @@ -69,7 +69,7 @@ class CascadePreprocessor: BasePreprocessor { process(views:fromViews) process(views:toViews) } - + func process(views: [UIView]) { for view in views { guard let (deltaTime, direction, delayMatchedViews) = context[view]?.cascade else { continue } @@ -87,7 +87,7 @@ class CascadePreprocessor: BasePreprocessor { for (i, subview) in sortedSubviews.enumerated() { let delay = TimeInterval(i) * deltaTime + initialDelay - func applyDelay(view:UIView) { + func applyDelay(view: UIView) { if context.pairedView(for: view) == nil { context[view]?.delay = delay } else if delayMatchedViews, let paired = context.pairedView(for: view) { @@ -98,7 +98,7 @@ class CascadePreprocessor: BasePreprocessor { applyDelay(view: subview) } } - + applyDelay(view: subview) } } diff --git a/Sources/Hero.swift b/Sources/Hero.swift index f74001d5..f28f6c15 100644 --- a/Sources/Hero.swift +++ b/Sources/Hero.swift @@ -43,7 +43,7 @@ public class Hero: HeroBaseController { /// Shared singleton object for controlling the transition public static let shared = Hero() - + // MARK: Properties /// destination view controller @@ -61,7 +61,7 @@ public class Hero: HeroBaseController { } } } - + /// a UIViewControllerContextTransitioning object provided by UIKit, /// might be nil when transitioning. This happens when calling heroReplaceViewController fileprivate weak var transitionContext: UIViewControllerContextTransitioning? @@ -80,19 +80,19 @@ public class Hero: HeroBaseController { fileprivate var inNavigationController = false fileprivate var inTabBarController = false - fileprivate var inContainerController:Bool { + fileprivate var inContainerController: Bool { return inNavigationController || inTabBarController } - fileprivate var toOverFullScreen:Bool { + fileprivate var toOverFullScreen: Bool { return !inContainerController && toViewController!.modalPresentationStyle == .overFullScreen } - fileprivate var fromOverFullScreen:Bool { + fileprivate var fromOverFullScreen: Bool { return !inContainerController && fromViewController!.modalPresentationStyle == .overFullScreen } - + fileprivate var toView: UIView { return toViewController!.view } fileprivate var fromView: UIView { return fromViewController!.view } - + fileprivate override init() { super.init() } } @@ -103,7 +103,6 @@ public extension Hero { defaultAnimation = .none } - /// Set the default animation for next transition /// This usually overrides rootView's heroModifiers during the transition /// @@ -112,7 +111,6 @@ public extension Hero { defaultAnimation = animation } - /// Set the container color for next transition /// /// - Parameter color: container color @@ -130,7 +128,7 @@ internal extension Hero { $0.heroWillStartTransition?() $0.heroWillStartAnimatingTo?(viewController: tvc) } - + closureProcessForHeroDelegate(vc: tvc) { $0.heroWillStartTransition?() $0.heroWillStartAnimatingFrom?(viewController: fvc) @@ -141,17 +139,17 @@ internal extension Hero { fullScreenSnapshot = transitionContainer.window!.snapshotView(afterScreenUpdates: true)! transitionContainer.window!.addSubview(fullScreenSnapshot) - if let oldSnapshots = fromViewController?.hero_storedSnapshots { + if let oldSnapshots = fromViewController?.heroStoredSnapshots { for snapshot in oldSnapshots { snapshot.removeFromSuperview() } - fromViewController?.hero_storedSnapshots = nil + fromViewController?.heroStoredSnapshots = nil } - if let oldSnapshots = toViewController?.hero_storedSnapshots { + if let oldSnapshots = toViewController?.heroStoredSnapshots { for snapshot in oldSnapshots { snapshot.removeFromSuperview() } - toViewController?.hero_storedSnapshots = nil + toViewController?.heroStoredSnapshots = nil } prepareForTransition() @@ -219,13 +217,13 @@ internal extension Hero { context.unhide(rootView: toView) context.removeSnapshots(rootView: toView) context.storeViewAlpha(rootView: fromView) - fromViewController!.hero_storedSnapshots = context.snapshots(rootView: fromView) + fromViewController!.heroStoredSnapshots = context.snapshots(rootView: fromView) } else if !finished && !presenting && fromOverFullScreen { // cancelled dismissing a overFullScreen VC context.unhide(rootView: fromView) context.removeSnapshots(rootView: fromView) context.storeViewAlpha(rootView: toView) - toViewController!.hero_storedSnapshots = context.snapshots(rootView: toView) + toViewController!.heroStoredSnapshots = context.snapshots(rootView: toView) } else { context.unhideAll() context.removeAllSnapshots() @@ -240,7 +238,7 @@ internal extension Hero { // bug: http://openradar.appspot.com/radar?id=5320103646199808 UIApplication.shared.keyWindow!.addSubview(presenting ? fromView : toView) } - + // use temp variables to remember these values // because we have to reset everything before calling // any delegate or completion block @@ -293,7 +291,7 @@ internal extension Hero { internal extension Hero { - func shift(direction: HeroAnimationType.Direction, appearing:Bool, size: CGSize? = nil, transpose: Bool = false) -> CGPoint { + func shift(direction: HeroAnimationType.Direction, appearing: Bool, size: CGSize? = nil, transpose: Bool = false) -> CGPoint { let size = size ?? container.bounds.size let rtn: CGPoint switch direction { @@ -424,16 +422,16 @@ internal extension Hero { // delegate helper fileprivate extension Hero { - func closureProcessForHeroDelegate(vc: T, closure: (HeroViewControllerDelegate)->()) { + func closureProcessForHeroDelegate(vc: T, closure: (HeroViewControllerDelegate) -> Void) { if let delegate = vc as? HeroViewControllerDelegate { closure(delegate) } - + if let navigationController = vc as? UINavigationController, let delegate = navigationController.topViewController as? HeroViewControllerDelegate { closure(delegate) } - + if let tabBarController = vc as? UITabBarController, let delegate = tabBarController.viewControllers?[tabBarController.selectedIndex] as? HeroViewControllerDelegate { closure(delegate) @@ -441,15 +439,6 @@ fileprivate extension Hero { } } - - - - - - - - - // MARK: UIKit Protocol Conformance /***************************** diff --git a/Sources/HeroBaseController.swift b/Sources/HeroBaseController.swift index af166689..ead44063 100644 --- a/Sources/HeroBaseController.swift +++ b/Sources/HeroBaseController.swift @@ -113,14 +113,13 @@ public class HeroBaseController: NSObject { } } - internal var finishing: Bool = true internal var processors: [HeroPreprocessor]! internal var animators: [HeroAnimator]! internal var plugins: [HeroPlugin]! - internal var animatingViews: [(fromViews:[UIView], toViews:[UIView])]! + internal var animatingViews: [(fromViews: [UIView], toViews: [UIView])]! internal static var enabledPlugins: [HeroPlugin.Type] = [] @@ -284,7 +283,6 @@ internal extension HeroBaseController { } } - /// Actually animate the views /// subclass should call `prepareForTransition` & `prepareForAnimation` before calling `animate` func animate() { diff --git a/Sources/HeroContext.swift b/Sources/HeroContext.swift index f961a212..f830d344 100644 --- a/Sources/HeroContext.swift +++ b/Sources/HeroContext.swift @@ -29,20 +29,20 @@ public class HeroContext { internal var viewAlphas = [UIView: CGFloat]() internal var targetStates = [UIView: HeroTargetState]() - internal var defaultCoordinateSpace:HeroCoordinateSpace = .local + internal var defaultCoordinateSpace: HeroCoordinateSpace = .local internal init(container: UIView) { self.container = container } - internal func set(fromViews:[UIView], toViews:[UIView]) { + internal func set(fromViews: [UIView], toViews: [UIView]) { self.fromViews = fromViews self.toViews = toViews process(views: fromViews, idMap: &heroIDToSourceView) process(views: toViews, idMap: &heroIDToDestinationView) } - internal func process(views:[UIView], idMap: inout [String: UIView]) { + internal func process(views: [UIView], idMap: inout [String: UIView]) { for view in views { if container.convert(view.bounds, from: view).intersects(container.bounds) { if let heroID = view.heroID { @@ -109,7 +109,7 @@ extension HeroContext { if let snapshot = snapshotViews[view] { return snapshot } - + var containerView = container let coordinateSpace = targetStates[view]?.coordinateSpace ?? defaultCoordinateSpace switch coordinateSpace { @@ -126,7 +126,7 @@ extension HeroContext { case .global: break } - + unhide(view: view) // capture a snapshot without alpha & cornerRadius @@ -136,8 +136,8 @@ extension HeroContext { view.alpha = 1 let snapshot: UIView - let snapshotType:HeroSnapshotType = self[view]?.snapshotType ?? .optimized - + let snapshotType: HeroSnapshotType = self[view]?.snapshotType ?? .optimized + switch snapshotType { case .normal: snapshot = view.snapshotView(afterScreenUpdates: true)! @@ -167,12 +167,12 @@ extension HeroContext { newBarView.tintColor = barView.tintColor newBarView.barTintColor = barView.barTintColor newBarView.clipsToBounds = false - + // take a snapshot without the background barView.layer.sublayers![0].opacity = 0 let realSnapshot = barView.snapshotView(afterScreenUpdates: true)! barView.layer.sublayers![0].opacity = 1 - + newBarView.addSubview(realSnapshot) snapshot = newBarView } else { @@ -190,7 +190,7 @@ extension HeroContext { if snapshotType != .noSnapshot { snapshot.layer.allowsGroupOpacity = false } - + view.layer.cornerRadius = oldCornerRadius view.alpha = oldAlpha @@ -279,7 +279,7 @@ extension HeroContext { snapshot.removeFromSuperview() } } - internal func removeSnapshots(rootView:UIView) { + internal func removeSnapshots(rootView: UIView) { if let snapshot = snapshotViews[rootView], snapshot != rootView { snapshot.removeFromSuperview() } @@ -297,16 +297,16 @@ extension HeroContext { return snapshots } internal func loadViewAlpha(rootView: UIView) { - if let storedAlpha = rootView.hero_storedAlpha { + if let storedAlpha = rootView.heroStoredAlpha { rootView.alpha = storedAlpha - rootView.hero_storedAlpha = nil + rootView.heroStoredAlpha = nil } for subview in rootView.subviews { loadViewAlpha(rootView: subview) } } internal func storeViewAlpha(rootView: UIView) { - rootView.hero_storedAlpha = viewAlphas[rootView] + rootView.heroStoredAlpha = viewAlphas[rootView] for subview in rootView.subviews { storeViewAlpha(rootView: subview) } diff --git a/Sources/HeroDebugView.swift b/Sources/HeroDebugView.swift index 433bd046..c2d0739b 100644 --- a/Sources/HeroDebugView.swift +++ b/Sources/HeroDebugView.swift @@ -40,7 +40,7 @@ class HeroDebugView: UIView { var delegate: HeroDebugViewDelegate? var panGR: UIPanGestureRecognizer! - + var pinchGR: UIPinchGestureRecognizer! var showControls: Bool = false { @@ -48,7 +48,7 @@ class HeroDebugView: UIView { layoutSubviews() } } - + var showOnTop: Bool = false var rotation: CGFloat = CGFloat(M_PI / 6) var scale: CGFloat = 0.6 @@ -56,7 +56,7 @@ class HeroDebugView: UIView { var progress: Float { return debugSlider.value } - + init(initialProcess: Float, showCurveButton: Bool, showOnTop: Bool) { super.init(frame:.zero) self.showOnTop = showOnTop @@ -97,7 +97,7 @@ class HeroDebugView: UIView { panGR = UIPanGestureRecognizer(target: self, action: #selector(pan)) panGR.delegate = self panGR.maximumNumberOfTouches = 1 - + addGestureRecognizer(panGR) pinchGR = UIPinchGestureRecognizer(target: self, action: #selector(pinch)) diff --git a/Sources/HeroDefaultAnimator.swift b/Sources/HeroDefaultAnimator.swift index e6304bea..03544c16 100644 --- a/Sources/HeroDefaultAnimator.swift +++ b/Sources/HeroDefaultAnimator.swift @@ -40,7 +40,7 @@ public class HeroDefaultAnimator: HeroAnimator { } return duration } - + public func apply(state: HeroTargetState, to view: UIView) { guard let context = viewContexts[view] else { print("HERO: unable to temporarily set to \(view). The view must be running at least one animation before it can be interactively changed") @@ -99,7 +99,7 @@ public class HeroDefaultAnimator: HeroAnimator { let viewContext = HeroDefaultAnimatorViewContext(animator:self, snapshot: snapshot, targetState: context[view]!, appearing: appearing) viewContexts[view] = viewContext } - + public func clean() { for vc in viewContexts.values { vc.clean() diff --git a/Sources/HeroDefaultAnimatorViewContext.swift b/Sources/HeroDefaultAnimatorViewContext.swift index 4cc385c9..f30b79f1 100644 --- a/Sources/HeroDefaultAnimatorViewContext.swift +++ b/Sources/HeroDefaultAnimatorViewContext.swift @@ -29,7 +29,7 @@ internal class HeroDefaultAnimatorViewContext { var duration: TimeInterval = 0 var targetState: HeroTargetState - var defaultTiming: (duration:TimeInterval, timingFunction:CAMediaTimingFunction)! + var defaultTiming: (duration: TimeInterval, timingFunction: CAMediaTimingFunction)! // computed var contentLayer: CALayer? { @@ -82,7 +82,7 @@ internal class HeroDefaultAnimatorViewContext { return overlayLayer! } - func overlayKeyFor(key:String) -> String? { + func overlayKeyFor(key: String) -> String? { if key.hasPrefix("overlay.") { var key = key key.removeSubrange(key.startIndex.. (duration:TimeInterval, timingFunction:CAMediaTimingFunction) { + func optimizedDurationAndTimingFunction() -> (duration: TimeInterval, timingFunction: CAMediaTimingFunction) { // timing function let fromPos = (state["position"]?.0 as? NSValue)?.cgPointValue ?? snapshot.layer.position let toPos = (state["position"]?.1 as? NSValue)?.cgPointValue ?? fromPos @@ -254,7 +254,7 @@ internal class HeroDefaultAnimatorViewContext { let realFromPos = CGPoint.zero.transform(fromTransform) + fromPos let realToPos = CGPoint.zero.transform(toTransform) + toPos - var timingFunction:CAMediaTimingFunction = .standard + var timingFunction: CAMediaTimingFunction = .standard if let container = container, !container.bounds.contains(realToPos) { // acceleration if leaving screen timingFunction = .acceleration @@ -293,7 +293,7 @@ internal class HeroDefaultAnimatorViewContext { duration = max(duration, neededTime + delay) } } - + func apply(state: HeroTargetState) { let targetState = viewState(targetState: state) for (key, targetValue) in targetState { @@ -350,7 +350,7 @@ internal class HeroDefaultAnimatorViewContext { overlayLayer = nil } - func currentValue(key:String) -> Any? { + func currentValue(key: String) -> Any? { if let key = overlayKeyFor(key: key) { return overlayLayer?.value(forKeyPath: key) } diff --git a/Sources/HeroIndependentController.swift b/Sources/HeroIndependentController.swift index 2899099e..0d94d1a3 100644 --- a/Sources/HeroIndependentController.swift +++ b/Sources/HeroIndependentController.swift @@ -27,7 +27,7 @@ public class HeroIndependentController: HeroBaseController { super.init() } - public func transition(rootView:UIView, fromViews:[UIView], toViews:[UIView], completion: ((Bool) -> Void)? = nil) { + public func transition(rootView: UIView, fromViews: [UIView], toViews: [UIView], completion: ((Bool) -> Void)? = nil) { transitionContainer = rootView completionCallback = completion diff --git a/Sources/HeroModifier.swift b/Sources/HeroModifier.swift index d44330da..aee3f81d 100644 --- a/Sources/HeroModifier.swift +++ b/Sources/HeroModifier.swift @@ -122,7 +122,7 @@ extension HeroModifier { } } - public static func translate(_ point:CGPoint, z: CGFloat = 0) -> HeroModifier { + public static func translate(_ point: CGPoint, z: CGFloat = 0) -> HeroModifier { return translate(x: point.x, y: point.y, z: z) } @@ -155,7 +155,6 @@ extension HeroModifier { } } - extension HeroModifier { /** Set the opacity for the view to animate from/to. @@ -306,7 +305,6 @@ extension HeroModifier { } } - /** Sets the duration of the animation for a given view to match the longest animation of the transition. */ @@ -436,7 +434,7 @@ extension HeroModifier { ignore all heroModifiers attributes for a view's direct subviews. */ public static var ignoreSubviewModifiers: HeroModifier = .ignoreSubviewModifiers() - + /** ignore all heroModifiers attributes for a view's subviews. - Parameters: @@ -447,7 +445,7 @@ extension HeroModifier { targetState.ignoreSubviewModifiers = recursive } } - + /** Will create snapshot optimized for different view type. For custom views or views with masking, useOptimizedSnapshot might create snapshots @@ -474,7 +472,7 @@ extension HeroModifier { public static var useLayerRenderSnapshot: HeroModifier = HeroModifier { targetState in targetState.snapshotType = .layerRender } - + /** Force Hero to not create any snapshot when animating this view. This will mess up the view hierarchy, therefore, view controllers have to rebuild diff --git a/Sources/HeroPlugin.swift b/Sources/HeroPlugin.swift index b76bbf0f..390b74e5 100644 --- a/Sources/HeroPlugin.swift +++ b/Sources/HeroPlugin.swift @@ -36,9 +36,9 @@ open class HeroPlugin: NSObject, HeroPreprocessor, HeroAnimator { When **requirePerFrameCallback** is **true**, the plugin will receive `seekTo` callback on every animation frame. Hence it is possible for the plugin to do per-frame animations without implementing `animate` & `resume` */ open var requirePerFrameCallback = false - + public override required init() {} - + /** Called before any animation. Override this method when you want to preprocess modifiers for views @@ -59,7 +59,7 @@ open class HeroPlugin: NSObject, HeroPreprocessor, HeroAnimator { */ open func process(fromViews: [UIView], toViews: [UIView]) {} - + /** - Returns: return true if the plugin can handle animating the view. - Parameters: @@ -70,7 +70,7 @@ open class HeroPlugin: NSObject, HeroPreprocessor, HeroAnimator { The view will also be hidden automatically during the animation. */ open func canAnimate(view: UIView, appearing: Bool) -> Bool { return false } - + /** Perform the animation. @@ -81,9 +81,9 @@ open class HeroPlugin: NSObject, HeroPreprocessor, HeroAnimator { - toViews: A flattened list of all views from destination ViewController (filtered by `canAnimate`) - Returns: The duration needed to complete the animation */ - + open func animate(fromViews: [UIView], toViews: [UIView]) -> TimeInterval { return 0 } - + /** Called when all animations are completed. diff --git a/Sources/HeroTargetState.swift b/Sources/HeroTargetState.swift index 49eff4dc..1843ac2e 100644 --- a/Sources/HeroTargetState.swift +++ b/Sources/HeroTargetState.swift @@ -22,27 +22,25 @@ import UIKit - public enum HeroSnapshotType { /// Will optimize for different type of views /// For custom views or views with masking, .optimizedDefault might create snapshots /// that appear differently than the actual view. /// In that case, use .normal or .slowRender to disable the optimization case optimized - + /// snapshotView(afterScreenUpdates:) case normal - + /// layer.render(in: currentContext) case layerRender - + /// will not create snapshot. animate the view directly. /// This will mess up the view hierarchy, therefore, view controllers have to rebuild /// its view structure after the transition finishes case noSnapshot } - public enum HeroCoordinateSpace { case global case local @@ -51,8 +49,8 @@ public enum HeroCoordinateSpace { public struct HeroTargetState { class HeroTargetStateWrapper { - var state:HeroTargetState - init(state:HeroTargetState) { + var state: HeroTargetState + init(state: HeroTargetState) { self.state = state } } @@ -76,7 +74,7 @@ public struct HeroTargetState { internal var masksToBounds: Bool? internal var displayShadow: Bool = true - internal var overlay: (color:CGColor, opacity:CGFloat)? + internal var overlay: (color: CGColor, opacity: CGFloat)? internal var spring: (CGFloat, CGFloat)? internal var delay: TimeInterval = 0 @@ -86,7 +84,7 @@ public struct HeroTargetState { internal var arc: CGFloat? internal var source: String? internal var cascade: (TimeInterval, CascadeDirection, Bool)? - + internal var ignoreSubviewModifiers: Bool? internal var coordinateSpace: HeroCoordinateSpace? internal var useScaleBasedSizeChange: Bool? diff --git a/Sources/HeroTypes.swift b/Sources/HeroTypes.swift index a5aa9d9d..747f8f36 100644 --- a/Sources/HeroTypes.swift +++ b/Sources/HeroTypes.swift @@ -39,11 +39,11 @@ public enum HeroAnimationType { indirect case selectBy(presenting:HeroAnimationType, dismissing:HeroAnimationType) case none - public var label:String? { + public var label: String? { let mirror = Mirror(reflecting: self) if let associated = mirror.children.first { let valuesMirror = Mirror(reflecting: associated.value) - if valuesMirror.children.count > 0 { + if !valuesMirror.children.isEmpty { let parameters = valuesMirror.children.map { ".\($0.value)" }.joined(separator: ",") return ".\(associated.label ?? "")(\(parameters))" } @@ -63,7 +63,7 @@ public protocol HeroAnimator { func canAnimate(view: UIView, appearing: Bool) -> Bool func animate(fromViews: [UIView], toViews: [UIView]) -> TimeInterval func clean() - + func seekTo(timePassed: TimeInterval) func resume(timePassed: TimeInterval, reverse: Bool) -> TimeInterval func apply(state: HeroTargetState, to view: UIView) @@ -77,7 +77,7 @@ public protocol HeroProgressUpdateObserver { @objc optional func heroWillStartAnimatingFrom(viewController: UIViewController) @objc optional func heroDidEndAnimatingFrom(viewController: UIViewController) @objc optional func heroDidCancelAnimatingFrom(viewController: UIViewController) - + @objc optional func heroWillStartTransition() @objc optional func heroDidEndTransition() @objc optional func heroDidCancelTransition() diff --git a/Sources/IgnoreSubviewModifiersPreprocessor.swift b/Sources/IgnoreSubviewModifiersPreprocessor.swift index 93d1126c..c49df8d0 100644 --- a/Sources/IgnoreSubviewModifiersPreprocessor.swift +++ b/Sources/IgnoreSubviewModifiersPreprocessor.swift @@ -27,7 +27,7 @@ class IgnoreSubviewModifiersPreprocessor: BasePreprocessor { process(views:fromViews) process(views:toViews) } - + func process(views: [UIView]) { for (viewIndex, view) in views.enumerated() { guard let recursive = context[view]?.ignoreSubviewModifiers else { continue } diff --git a/Sources/SourcePreprocessor.swift b/Sources/SourcePreprocessor.swift index a56e9b6e..0a746eec 100644 --- a/Sources/SourcePreprocessor.swift +++ b/Sources/SourcePreprocessor.swift @@ -35,12 +35,12 @@ class SourcePreprocessor: BasePreprocessor { prepareFor(view: tv, targetView: fv) } } - + func prepareFor(view: UIView, targetView: UIView) { let targetPos = context.container.convert(targetView.layer.position, from: targetView.superview!) var state = context[view]! - + // use global coordinate space since over target position is converted from the global container state.coordinateSpace = .global diff --git a/Sources/UIKit+Hero.swift b/Sources/UIKit+Hero.swift index e303ee95..f5ebc26c 100644 --- a/Sources/UIKit+Hero.swift +++ b/Sources/UIKit+Hero.swift @@ -43,7 +43,7 @@ public extension UIView { get { return objc_getAssociatedObject(self, &AssociatedKeys.heroID) as? String } set { objc_setAssociatedObject(self, &AssociatedKeys.heroID, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } - + /** Use **heroModifiers** to specify animations alongside the main transition. Checkout `HeroModifier.swift` for available modifiers. */ @@ -105,11 +105,11 @@ public extension UIView { } internal var flattenedViewHierarchy: [UIView] { - return [self] + subviews.flatMap{ $0.flattenedViewHierarchy } + return [self] + subviews.flatMap { $0.flattenedViewHierarchy } } /// Used for .overFullScreen presentation - internal var hero_storedAlpha: CGFloat? { + internal var heroStoredAlpha: CGFloat? { get { if let doubleValue = (objc_getAssociatedObject(self, &AssociatedKeys.heroStoredAlpha) as? NSNumber)?.doubleValue { return CGFloat(doubleValue) @@ -179,7 +179,7 @@ public extension UIViewController { } /// used for .overFullScreen presentation - internal var hero_storedSnapshots: [UIView]? { + internal var heroStoredSnapshots: [UIView]? { get { return objc_getAssociatedObject(self, &AssociatedKeys.heroStoredSnapshots) as? [UIView] } @@ -192,13 +192,12 @@ public extension UIViewController { @IBAction public func ht_dismiss(_ sender: UIView) { hero_dismissViewController() } - + @available(*, deprecated: 0.1.4, message: "use hero_replaceViewController(with:) instead") public func heroReplaceViewController(with next: UIViewController) { hero_replaceViewController(with: next) } - - + /** Dismiss the current view controller with animation. Will perform a navigationController.popViewController if the current view controller is contained inside a navigationController @@ -210,42 +209,42 @@ public extension UIViewController { dismiss(animated: true, completion: nil) } } - + /** Unwind to the root view controller using Hero */ @IBAction public func hero_unwindToRootViewController() { hero_unwindToViewController { $0.presentingViewController == nil } } - + /** Unwind to a specific view controller using Hero */ public func hero_unwindToViewController(_ toViewController: UIViewController) { hero_unwindToViewController { $0 == toViewController } } - + /** Unwind to a view controller that responds to the given selector using Hero */ public func hero_unwindToViewController(withSelector: Selector) { hero_unwindToViewController { $0.responds(to: withSelector) } } - + /** Unwind to a view controller with given class using Hero */ public func hero_unwindToViewController(withClass: AnyClass) { hero_unwindToViewController { $0.isKind(of: withClass) } } - + /** Unwind to a view controller that the matchBlock returns true on. */ public func hero_unwindToViewController(withMatchBlock: (UIViewController) -> Bool) { - var target:UIViewController? = nil - var current:UIViewController? = self - + var target: UIViewController? = nil + var current: UIViewController? = self + while target == nil && current != nil { if let childViewControllers = (current as? UINavigationController)?.childViewControllers ?? current!.navigationController?.childViewControllers { for vc in childViewControllers.reversed() { @@ -262,14 +261,14 @@ public extension UIViewController { } } } - + if let target = target { if target.presentedViewController != nil { let _ = target.navigationController?.popToViewController(target, animated: false) - + let fromVC = self.navigationController ?? self let toVC = target.navigationController ?? target - + if target.presentedViewController != fromVC { // UIKit's UIViewController.dismiss will jump to target.presentedViewController then perform the dismiss. // We overcome this behavior by inserting a snapshot into target.presentedViewController @@ -278,7 +277,7 @@ public extension UIViewController { let snapshotView = fromVC.view.snapshotView(afterScreenUpdates: true)! toVC.presentedViewController!.view.addSubview(snapshotView) } - + toVC.dismiss(animated: true, completion: nil) } else { let _ = target.navigationController?.popToViewController(target, animated: true) @@ -307,7 +306,7 @@ public extension UIViewController { Hero.shared.transition(from: self, to: next, in: container) { finished in if finished { UIApplication.shared.keyWindow?.addSubview(next.view) - + if let parentVC = parentVC { self.dismiss(animated: false) { parentVC.present(next, animated: false, completion:nil) @@ -320,7 +319,7 @@ public extension UIViewController { } } - public func hero_presentOnTop(viewController:UIViewController, frame:CGRect) { + public func hero_presentOnTop(viewController: UIViewController, frame: CGRect) { var oldViews = view.flattenedViewHierarchy oldViews.removeFirst() let hero = HeroIndependentController() @@ -344,15 +343,15 @@ internal extension UIImage { } internal extension UIColor { - var components:(r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) { - var r:CGFloat = 0 - var g:CGFloat = 0 - var b:CGFloat = 0 - var a:CGFloat = 0 + var components:(r:CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) { + var r: CGFloat = 0 + var g: CGFloat = 0 + var b: CGFloat = 0 + var a: CGFloat = 0 getRed(&r, green: &g, blue: &b, alpha: &a) return (r, g, b, a) } - var alphaComponent:CGFloat { + var alphaComponent: CGFloat { return components.a } }