Skip to content

Commit

Permalink
expand functional
Browse files Browse the repository at this point in the history
  • Loading branch information
winddpan committed Jul 20, 2017
1 parent 2ca0935 commit 38bb925
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 62 deletions.
6 changes: 3 additions & 3 deletions Demo/Demo/ImageDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ extension ImageDetailViewController: ZoomTransitionSourceDelegate {
return smallImageView1.convert(smallImageView1.bounds, to: view)
}

func transitionSourceWillBegin() {
func transitionSourceWillBegin(forward: Bool) {
smallImageView1.isHidden = true
}

func transitionSourceDidEnd() {
func transitionSourceDidEnd(forward: Bool) {
smallImageView1.isHidden = false
}

Expand All @@ -77,7 +77,7 @@ extension ImageDetailViewController: ZoomTransitionDestinationDelegate {
}
}

func transitionDestinationWillBegin() {
func transitionDestinationWillBegin(forward: Bool) {
largeImageView.isHidden = true
}

Expand Down
16 changes: 14 additions & 2 deletions Demo/Demo/ImageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,27 @@ extension ImageListViewController: ZoomTransitionSourceDelegate {
return selectedImageView.convert(selectedImageView.bounds, to: view)
}

func transitionSourceWillBegin() {
func transitionSourceWillBegin(forward: Bool) {
selectedImageView?.isHidden = true
}

func transitionSourceDidEnd() {
func transitionSourceDidEnd(forward: Bool) {
selectedImageView?.isHidden = false
}

func transitionSourceDidCancel() {
selectedImageView?.isHidden = false
}

func zoomDuration() -> TimeInterval {
return 0.4
}

func zoomAnimation(animations: @escaping () -> Void, completion: @escaping () -> Void) {
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2, options: .curveEaseInOut, animations: {
animations()
}) { (_) in
completion()
}
}
}
7 changes: 7 additions & 0 deletions ZoomTransitioning/ZoomInteractiveTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public final class ZoomInteractiveTransition: UIPercentDrivenInteractiveTransiti
update(0.0)
}
cancel()

if let viewController = viewController as? ZoomTransitionDestinationDelegate {
viewController.transitionDestinationDidCancel?()
}
if let viewController = navigationController?.viewControllers.last as? ZoomTransitionSourceDelegate {
viewController.transitionSourceDidCancel?()
}
}
interactive = false
default:
Expand Down
2 changes: 0 additions & 2 deletions ZoomTransitioning/ZoomNavigationControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,4 @@ extension ZoomNavigationControllerDelegate: UINavigationControllerDelegate {
}
return nil
}


}
2 changes: 1 addition & 1 deletion ZoomTransitioning/ZoomTransitionDestinationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
@objc public protocol ZoomTransitionDestinationDelegate: NSObjectProtocol {

func transitionDestinationImageViewFrame(forward: Bool) -> CGRect
@objc optional func transitionDestinationWillBegin()
@objc optional func transitionDestinationWillBegin(forward: Bool)
@objc optional func transitionDestinationDidEnd(transitioningImageView imageView: UIImageView)
@objc optional func transitionDestinationDidCancel()
}
7 changes: 5 additions & 2 deletions ZoomTransitioning/ZoomTransitionSourceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import UIKit

func transitionSourceImageView() -> UIImageView
func transitionSourceImageViewFrame(forward: Bool) -> CGRect
@objc optional func transitionSourceWillBegin()
@objc optional func transitionSourceDidEnd()
@objc optional func transitionSourceWillBegin(forward: Bool)
@objc optional func transitionSourceDidEnd(forward: Bool)
@objc optional func transitionSourceDidCancel()

@objc optional func zoomDuration() -> TimeInterval
@objc optional func zoomAnimation(animations: @escaping () -> Void, completion: @escaping () -> Void)
}
105 changes: 53 additions & 52 deletions ZoomTransitioning/ZoomTransitioning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
import UIKit

public final class ZoomTransitioning: NSObject {

static let transitionDuration: TimeInterval = 0.3
fileprivate let source: ZoomTransitionSourceDelegate
fileprivate let destination: ZoomTransitionDestinationDelegate
fileprivate let forward: Bool

open func runAnimation(animations: @escaping () -> Swift.Void, completion: @escaping (() -> Swift.Void)) {
if source.responds(to: #selector(ZoomTransitionSourceDelegate.zoomAnimation)) {
source.zoomAnimation?(animations: animations, completion: completion)
} else {
UIView.animate(withDuration: source.zoomDuration?() ?? 0.3, delay: 0, options: .curveEaseInOut, animations: {
animations()
}) { _ in
completion()
}
}
}

required public init(source: ZoomTransitionSourceDelegate, destination: ZoomTransitionDestinationDelegate, forward: Bool) {
self.source = source
self.destination = destination
Expand All @@ -30,7 +40,7 @@ public final class ZoomTransitioning: NSObject {
extension ZoomTransitioning: UIViewControllerAnimatedTransitioning {

public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return ZoomTransitioning.transitionDuration
return source.zoomDuration?() ?? 0.3
}

public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
Expand Down Expand Up @@ -58,29 +68,24 @@ extension ZoomTransitioning: UIViewControllerAnimatedTransitioning {
containerView.insertSubview(destinationView, belowSubview: sourceView)
containerView.addSubview(transitioningImageView)

source.transitionSourceWillBegin?()
destination.transitionDestinationWillBegin?()

UIView.animate(
withDuration: ZoomTransitioning.transitionDuration,
delay: 0.0,
options: .curveEaseOut,
animations: {
sourceView.alpha = 0.0
destinationView.alpha = 1.0
transitioningImageView.frame = self.destination.transitionDestinationImageViewFrame(forward: self.forward)
},
completion: { _ in
sourceView.alpha = 1.0
transitioningImageView.alpha = 0.0
transitioningImageView.removeFromSuperview()

self.source.transitionSourceDidEnd?()
self.destination.transitionDestinationDidEnd?(transitioningImageView: transitioningImageView)

let completed = !transitionContext.transitionWasCancelled
transitionContext.completeTransition(completed)
})
source.transitionSourceWillBegin?(forward: self.forward)
destination.transitionDestinationWillBegin?(forward: self.forward)

self.runAnimation(animations: {
sourceView.alpha = 0.0
destinationView.alpha = 1.0
transitioningImageView.frame = self.destination.transitionDestinationImageViewFrame(forward: self.forward)
}) {
sourceView.alpha = 1.0
transitioningImageView.alpha = 0.0
transitioningImageView.removeFromSuperview()

self.source.transitionSourceDidEnd?(forward: self.forward)
self.destination.transitionDestinationDidEnd?(transitioningImageView: transitioningImageView)

let completed = !transitionContext.transitionWasCancelled
transitionContext.completeTransition(completed)
}
}

private func animateTransitionForPop(_ transitionContext: UIViewControllerContextTransitioning) {
Expand All @@ -100,36 +105,32 @@ extension ZoomTransitioning: UIViewControllerAnimatedTransitioning {
containerView.insertSubview(sourceView, belowSubview: destinationView)
containerView.addSubview(transitioningImageView)

source.transitionSourceWillBegin?()
destination.transitionDestinationWillBegin?()
source.transitionSourceWillBegin?(forward: self.forward)
destination.transitionDestinationWillBegin?(forward: self.forward)

if transitioningImageView.frame.maxY < 0.0 {
transitioningImageView.frame.origin.y = -transitioningImageView.frame.height
}
UIView.animate(
withDuration: ZoomTransitioning.transitionDuration,
delay: 0.0,
options: .curveEaseOut,
animations: {
destinationView.alpha = 0.0
sourceView.alpha = 1.0
transitioningImageView.frame = self.source.transitionSourceImageViewFrame(forward: self.forward)
},
completion: { _ in
destinationView.alpha = 1.0
transitioningImageView.removeFromSuperview()

self.source.transitionSourceDidEnd?()
self.destination.transitionDestinationDidEnd?(transitioningImageView: transitioningImageView)

let completed: Bool
if #available(iOS 10.0, *) {
completed = true
} else {
completed = !transitionContext.transitionWasCancelled
}
transitionContext.completeTransition(completed)
})

self.runAnimation(animations: {
destinationView.alpha = 0.0
sourceView.alpha = 1.0
transitioningImageView.frame = self.source.transitionSourceImageViewFrame(forward: self.forward)
}) {
destinationView.alpha = 1.0
transitioningImageView.removeFromSuperview()

self.source.transitionSourceDidEnd?(forward: self.forward)
self.destination.transitionDestinationDidEnd?(transitioningImageView: transitioningImageView)

let completed: Bool
if #available(iOS 10.0, *) {
completed = true
} else {
completed = !transitionContext.transitionWasCancelled
}
transitionContext.completeTransition(completed)
}
}

private func transitioningPushImageView() -> UIImageView {
Expand Down

0 comments on commit 38bb925

Please sign in to comment.