Skip to content

Commit

Permalink
added delegation support to transition
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetkgunay committed May 24, 2017
1 parent 9a1ac7c commit 8bb139c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 99 deletions.
85 changes: 52 additions & 33 deletions AKGPushAnimator/AKGPushAnimator/AKGPushAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@

import UIKit

public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
@objc public protocol AKGPushAnimatorDelegate {

@objc optional func beganTransition()
@objc optional func finishedTransition()
}


public class AKGPushAnimator: NSObject {

public var isReverseTransition = false

open var delegate : AKGPushAnimatorDelegate?

// MARK: Variables with Getters

var animatorScreenWidth : CGFloat {
Expand Down Expand Up @@ -62,6 +71,42 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
}
}

fileprivate func animate(withTransitionContext transitionContext:UIViewControllerContextTransitioning,
toView: UIView,
fromView: UIView,
duration: TimeInterval,
delay: TimeInterval,
options: UIViewAnimationOptions = [],
animations: @escaping () -> Swift.Void) {

UIView.animate(withDuration: duration,
delay: delay,
options: options,
animations: animations) { (finished) in

if (transitionContext.transitionWasCancelled) {
toView.removeFromSuperview()
} else {
fromView.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

self.delegate?.finishedTransition?()
}
}

fileprivate func addShadowToView(_ toView:UIView!) -> Swift.Void {

toView.layer.shadowOpacity = AKGPushAnimatorConstants.Common.shadowOpacity
toView.layer.shadowOffset = CGSize(width:0, height:3)
toView.layer.shadowColor = AKGPushAnimatorConstants.Common.shadowColor.cgColor
let shadowRect: CGRect = toView.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom
toView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
}
}

extension AKGPushAnimator: UIViewControllerAnimatedTransitioning {

// MARK: UIViewControllerAnimatedTransitioning

public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
Expand All @@ -77,11 +122,9 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let toView = toVC.view,
let fromView = fromVC.view else { return }

toView.layer.shadowOpacity = AKGPushAnimatorConstants.Common.shadowOpacity
toView.layer.shadowOffset = CGSize(width:0, height:3)
toView.layer.shadowColor = AKGPushAnimatorConstants.Common.shadowColor.cgColor
let shadowRect: CGRect = toView.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom
toView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
addShadowToView(toView)

delegate?.beganTransition?()

if !isReverseTransition {

Expand All @@ -100,12 +143,11 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
delay: 0,
options: AKGPushAnimatorConstants.Push.animateOption,
animations: {

fromView.frame = self.fromViewPushedFrame
toView.frame = self.toViewPushedFrame
})
toView.frame = self.toViewPushedFrame})
}
else {

containerView.addSubview(toView)
containerView.addSubview(fromView)

Expand All @@ -121,32 +163,9 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
delay: 0,
options: AKGPushAnimatorConstants.Pop.animateOption,
animations: {

fromView.frame = self.fromViewPopedFrame
toView.frame = self.toViewPopedFrame
})
toView.frame = self.toViewPopedFrame })
}
}
}

private func animate(withTransitionContext transitionContext:UIViewControllerContextTransitioning,
toView: UIView,
fromView: UIView,
duration: TimeInterval,
delay: TimeInterval,
options: UIViewAnimationOptions = [],
animations: @escaping () -> Swift.Void) {

UIView.animate(withDuration: duration,
delay: delay,
options: options,
animations: animations) { (finished) in

if (transitionContext.transitionWasCancelled) {
toView.removeFromSuperview()
} else {
fromView.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
16 changes: 16 additions & 0 deletions AKGPushAnimator/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FirstViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
pushAnimator.delegate = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
Expand All @@ -23,4 +24,19 @@ class FirstViewController: BaseViewController {
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

deinit {
pushAnimator.delegate = nil
}
}

extension FirstViewController: AKGPushAnimatorDelegate {

func beganTransition() {
print("began transition")
}

func finishedTransition() {
print("finished transition")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@

import UIKit

public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
@objc public protocol AKGPushAnimatorDelegate {

@objc optional func beganTransition()
@objc optional func finishedTransition()
}


public class AKGPushAnimator: NSObject {

public var isReverseTransition = false

open var delegate : AKGPushAnimatorDelegate?

// MARK: Variables with Getters

var animatorScreenWidth : CGFloat {
Expand Down Expand Up @@ -62,6 +71,42 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
}
}

fileprivate func animate(withTransitionContext transitionContext:UIViewControllerContextTransitioning,
toView: UIView,
fromView: UIView,
duration: TimeInterval,
delay: TimeInterval,
options: UIViewAnimationOptions = [],
animations: @escaping () -> Swift.Void) {

UIView.animate(withDuration: duration,
delay: delay,
options: options,
animations: animations) { (finished) in

if (transitionContext.transitionWasCancelled) {
toView.removeFromSuperview()
} else {
fromView.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

self.delegate?.finishedTransition?()
}
}

fileprivate func addShadowToView(_ toView:UIView!) -> Swift.Void {

toView.layer.shadowOpacity = AKGPushAnimatorConstants.Common.shadowOpacity
toView.layer.shadowOffset = CGSize(width:0, height:3)
toView.layer.shadowColor = AKGPushAnimatorConstants.Common.shadowColor.cgColor
let shadowRect: CGRect = toView.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom
toView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
}
}

extension AKGPushAnimator: UIViewControllerAnimatedTransitioning {

// MARK: UIViewControllerAnimatedTransitioning

public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
Expand All @@ -77,11 +122,9 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let toView = toVC.view,
let fromView = fromVC.view else { return }

toView.layer.shadowOpacity = AKGPushAnimatorConstants.Common.shadowOpacity
toView.layer.shadowOffset = CGSize(width:0, height:3)
toView.layer.shadowColor = AKGPushAnimatorConstants.Common.shadowColor.cgColor
let shadowRect: CGRect = toView.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom
toView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath
addShadowToView(toView)

delegate?.beganTransition?()

if !isReverseTransition {

Expand All @@ -100,12 +143,11 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
delay: 0,
options: AKGPushAnimatorConstants.Push.animateOption,
animations: {

fromView.frame = self.fromViewPushedFrame
toView.frame = self.toViewPushedFrame
})
toView.frame = self.toViewPushedFrame})
}
else {

containerView.addSubview(toView)
containerView.addSubview(fromView)

Expand All @@ -121,32 +163,9 @@ public class AKGPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
delay: 0,
options: AKGPushAnimatorConstants.Pop.animateOption,
animations: {

fromView.frame = self.fromViewPopedFrame
toView.frame = self.toViewPopedFrame
})
toView.frame = self.toViewPopedFrame })
}
}
}

private func animate(withTransitionContext transitionContext:UIViewControllerContextTransitioning,
toView: UIView,
fromView: UIView,
duration: TimeInterval,
delay: TimeInterval,
options: UIViewAnimationOptions = [],
animations: @escaping () -> Swift.Void) {

UIView.animate(withDuration: duration,
delay: delay,
options: options,
animations: animations) { (finished) in

if (transitionContext.transitionWasCancelled) {
toView.removeFromSuperview()
} else {
fromView.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FirstViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
pushAnimator.delegate = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
Expand All @@ -23,4 +24,19 @@ class FirstViewController: BaseViewController {
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

deinit {
pushAnimator.delegate = nil
}
}

extension FirstViewController: AKGPushAnimatorDelegate {

func beganTransition() {
print("began transition")
}

func finishedTransition() {
print("finished transition")
}
}
Loading

0 comments on commit 8bb139c

Please sign in to comment.