-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMCMHeaderAnimated.swift
171 lines (121 loc) · 5.91 KB
/
MCMHeaderAnimated.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// MCMHeaderAnimated.swift
// MCMHeaderAnimated
//
// Created by Mathias Carignani on 5/19/15.
// Copyright (c) 2015 Mathias Carignani. All rights reserved.
//
import UIKit
@objc public protocol MCMHeaderAnimatedDelegate {
func headerView() -> UIView
func headerCopy(subview: UIView) -> UIView
}
public class MCMHeaderAnimated: UIPercentDrivenInteractiveTransition {
public var transitionMode: TransitionMode = .Present
public var transitionInteracted: Bool = false
private var headerFromFrame: CGRect! = nil
private var headerToFrame: CGRect! = nil
private var enterPanGesture: UIPanGestureRecognizer!
public var destinationViewController: UIViewController! {
didSet {
self.enterPanGesture = UIPanGestureRecognizer()
self.enterPanGesture.addTarget(self, action: #selector(self.handleOnstagePan(_:)))
self.destinationViewController.view.addGestureRecognizer(self.enterPanGesture)
self.transitionInteracted = true
}
}
public enum TransitionMode: Int {
case Present, Dismiss
}
func handleOnstagePan(pan: UIPanGestureRecognizer){
let translation = pan.translationInView(pan.view!)
let d = translation.y / CGRectGetHeight(pan.view!.bounds) * 1.5
switch (pan.state) {
case UIGestureRecognizerState.Began:
self.destinationViewController.dismissViewControllerAnimated(true, completion: nil)
break
case .Changed:
self.updateInteractiveTransition(d)
break
default: // .Ended, .Cancelled, .Failed ...
self.finishInteractiveTransition()
}
}
}
// MARK: - UIViewControllerAnimatedTransitioning
extension MCMHeaderAnimated: UIViewControllerAnimatedTransitioning {
public func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.65
}
public func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let fromController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let duration = self.transitionDuration(transitionContext)
fromView.setNeedsLayout()
fromView.layoutIfNeeded()
toView.setNeedsLayout()
toView.layoutIfNeeded()
let alpha: CGFloat = 0.1
let offScreenBottom = CGAffineTransformMakeTranslation(0, container!.frame.height)
// Prepare header
let headerTo = (toController as! MCMHeaderAnimatedDelegate).headerView()
let headerFrom = (fromController as! MCMHeaderAnimatedDelegate).headerView()
if self.transitionMode == .Present {
self.headerToFrame = headerTo.superview!.convertRect(headerTo.frame, toView: nil)
self.headerFromFrame = headerFrom.superview!.convertRect(headerFrom.frame, toView: nil)
}
headerFrom.alpha = 0
headerTo.alpha = 0
let headerIntermediate = (fromController as! MCMHeaderAnimatedDelegate).headerCopy(headerFrom)
headerIntermediate.frame = self.transitionMode == .Present ? self.headerFromFrame : self.headerToFrame
if self.transitionMode == .Present {
toView.transform = offScreenBottom
container!.addSubview(fromView)
container!.addSubview(toView)
container!.addSubview(headerIntermediate)
} else {
toView.alpha = alpha
container!.addSubview(toView)
container!.addSubview(fromView)
container!.addSubview(headerIntermediate)
}
// Perform de animation
UIView.animateWithDuration(duration, delay: 0.0, options: [], animations: {
if self.transitionMode == .Present {
fromView.alpha = alpha
toView.transform = CGAffineTransformIdentity
headerIntermediate.frame = self.headerToFrame
} else {
fromView.transform = offScreenBottom
toView.alpha = 1.0
headerIntermediate.frame = self.headerFromFrame
}
}, completion: { finished in
headerIntermediate.removeFromSuperview()
headerTo.alpha = 1
headerFrom.alpha = 1
transitionContext.completeTransition(true)
})
}
}
extension MCMHeaderAnimated {
public override func startInteractiveTransition(transitionContext: UIViewControllerContextTransitioning) {
super.startInteractiveTransition(transitionContext)
}
}
extension MCMHeaderAnimated: UIViewControllerTransitioningDelegate {
public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.transitionMode = .Present
return self
}
public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.transitionMode = .Dismiss
return self
}
public func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return self.transitionInteracted ? self : nil
}
}