@@ -14,11 +14,9 @@ var AssociatedObjectHandle: UInt8 = 0
14
14
15
15
public extension UIViewController {
16
16
public func presentFilePreviewController( viewControllerToPresent controller: UIViewController , fromView: UIView ? ) {
17
- if let fromView = fromView {
18
- let transitionDelegate = TransitionDelegate ( fromView: fromView)
19
- controller. transitioningDelegate = transitionDelegate
20
- controller. fp_transitionDelegate = transitionDelegate
21
- }
17
+ let transitionDelegate = TransitionDelegate ( fromView: fromView)
18
+ controller. transitioningDelegate = transitionDelegate
19
+ controller. fp_transitionDelegate = transitionDelegate
22
20
present ( controller, animated: true , completion: nil )
23
21
}
24
22
@@ -42,7 +40,7 @@ public extension UIViewController {
42
40
open class TransitionDelegate : NSObject , UIViewControllerTransitioningDelegate {
43
41
open var fromView : UIView ?
44
42
45
- init ( fromView: UIView ) {
43
+ init ( fromView: UIView ? ) {
46
44
super. init ( )
47
45
self . fromView = fromView
48
46
}
@@ -63,8 +61,8 @@ open class TransitionDelegate: NSObject, UIViewControllerTransitioningDelegate {
63
61
}
64
62
65
63
open class PresentAnimation : NSObject , UIViewControllerAnimatedTransitioning {
66
- var fromView : UIView !
67
- public init ( fromView: UIView ) {
64
+ var fromView : UIView ?
65
+ public init ( fromView: UIView ? ) {
68
66
super. init ( )
69
67
self . fromView = fromView
70
68
}
@@ -75,29 +73,43 @@ open class PresentAnimation: NSObject, UIViewControllerAnimatedTransitioning {
75
73
76
74
open func animateTransition( using transitionContext: UIViewControllerContextTransitioning ) {
77
75
let container = transitionContext. containerView
78
- let toVC = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . to) !
79
-
80
- container. addSubview ( toVC. view)
81
- let fromFrame = container. convert ( fromView. frame, from: fromView. superview)
82
- let toFrame = transitionContext. finalFrame ( for: toVC)
83
-
84
- let scale = CGAffineTransform ( scaleX: fromFrame. width/ toFrame. width, y: fromFrame. height/ toFrame. height)
85
- let translation = CGAffineTransform ( translationX: fromFrame. midX - toFrame. midX, y: fromFrame. midY - toFrame. midY)
86
- toVC. view. transform = scale. concatenating ( translation)
87
- toVC. view. alpha = 0
88
-
89
- UIView . animate ( withDuration: PresentDuration, delay: 0 , options: UIViewAnimationOptions ( ) , animations: {
90
- toVC. view. transform = CGAffineTransform ( scaleX: 1.0 , y: 1.0 )
91
- toVC. view. alpha = 1
76
+ guard let toViewController = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . to) ,
77
+ let fromViewController = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . from) else {
78
+ return
79
+ }
80
+ if let fromView = fromView {
81
+ container. addSubview ( toViewController. view)
82
+ let fromFrame = container. convert ( fromView. frame, from: fromView. superview)
83
+ let toFrame = transitionContext. finalFrame ( for: toViewController)
84
+
85
+ let scale = CGAffineTransform ( scaleX: fromFrame. width/ toFrame. width, y: fromFrame. height/ toFrame. height)
86
+ let translation = CGAffineTransform ( translationX: fromFrame. midX - toFrame. midX, y: fromFrame. midY - toFrame. midY)
87
+ toViewController. view. transform = scale. concatenating ( translation)
88
+ toViewController. view. alpha = 0
89
+
90
+ UIView . animate ( withDuration: PresentDuration, delay: 0 , options: UIViewAnimationOptions ( ) , animations: {
91
+ toViewController. view. transform = CGAffineTransform ( scaleX: 1.0 , y: 1.0 )
92
+ toViewController. view. alpha = 1
93
+ } ) { ( _) in
94
+ transitionContext. completeTransition ( !transitionContext. transitionWasCancelled)
95
+ }
96
+ } else {
97
+ var frame = fromViewController. view. frame
98
+ frame. origin. y = frame. size. height
99
+ toViewController. view. frame = frame
100
+ container. addSubview ( toViewController. view)
101
+ UIView . animate ( withDuration: PresentDuration, delay: 0 , options: . curveEaseIn, animations: {
102
+ toViewController. view. frame = fromViewController. view. frame
92
103
} ) { ( _) in
93
104
transitionContext. completeTransition ( !transitionContext. transitionWasCancelled)
105
+ }
94
106
}
95
107
}
96
108
}
97
109
98
110
open class DismissAnimation : NSObject , UIViewControllerAnimatedTransitioning {
99
- var toView : UIView !
100
- public init ( toView: UIView ) {
111
+ var toView : UIView ?
112
+ public init ( toView: UIView ? ) {
101
113
super. init ( )
102
114
self . toView = toView
103
115
}
@@ -108,23 +120,47 @@ open class DismissAnimation: NSObject, UIViewControllerAnimatedTransitioning {
108
120
109
121
open func animateTransition( using transitionContext: UIViewControllerContextTransitioning ) {
110
122
let container = transitionContext. containerView
111
- let fromVC = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . from) !
112
- let toVC = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . to) !
113
-
114
- toVC. view. frame = transitionContext. finalFrame ( for: toVC)
115
- container. addSubview ( toVC. view)
116
- container. addSubview ( fromVC. view)
117
- let toFrame = container. convert ( toView. frame, from: toView. superview)
118
- let fromFrame = transitionContext. finalFrame ( for: fromVC)
119
-
120
- let scale = CGAffineTransform ( scaleX: toFrame. width/ fromFrame. width, y: toFrame. height/ fromFrame. height)
121
- let translation = CGAffineTransform ( translationX: toFrame. midX - fromFrame. midX, y: toFrame. midY - fromFrame. midY)
122
- UIView . animate ( withDuration: DismissDuration, delay: 0 , options: . curveEaseOut, animations: {
123
- fromVC. view. alpha = 0
124
- fromVC. view. transform = scale. concatenating ( translation)
123
+ guard let toViewController = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . to) ,
124
+ let fromViewController = transitionContext. viewController ( forKey: UITransitionContextViewControllerKey . from) else {
125
+ return
126
+ }
127
+
128
+ func willDismiss( ) {
129
+ if let navigationViewController = fromViewController as? UINavigationController ,
130
+ let filePreviewController = navigationViewController. viewControllers. last as? FilePreviewController {
131
+ filePreviewController. willDismiss ( )
132
+ } else {
133
+ ( fromViewController as? FilePreviewController ) ? . willDismiss ( )
134
+ }
135
+ }
136
+
137
+ if let toView = toView {
138
+ toViewController. view. frame = transitionContext. finalFrame ( for: toViewController)
139
+ container. addSubview ( toViewController. view)
140
+ container. addSubview ( fromViewController. view)
141
+ let toFrame = container. convert ( toView. frame, from: toView. superview)
142
+ let fromFrame = transitionContext. finalFrame ( for: fromViewController)
143
+
144
+ let scale = CGAffineTransform ( scaleX: toFrame. width/ fromFrame. width, y: toFrame. height/ fromFrame. height)
145
+ let translation = CGAffineTransform ( translationX: toFrame. midX - fromFrame. midX, y: toFrame. midY - fromFrame. midY)
146
+ UIView . animate ( withDuration: DismissDuration, delay: 0 , options: . curveEaseOut, animations: {
147
+ fromViewController. view. alpha = 0
148
+ fromViewController. view. transform = scale. concatenating ( translation)
149
+ } ) { ( _) in
150
+ willDismiss ( )
151
+ fromViewController. view. removeFromSuperview ( )
152
+ transitionContext. completeTransition ( !transitionContext. transitionWasCancelled)
153
+ }
154
+ } else {
155
+ var frame = fromViewController. view. frame
156
+ frame. origin. y = frame. size. height
157
+ UIView . animate ( withDuration: DismissDuration, delay: 0 , options: . curveEaseIn, animations: {
158
+ fromViewController. view. frame = frame
125
159
} ) { ( _) in
126
- fromVC. view. removeFromSuperview ( )
160
+ willDismiss ( )
161
+ fromViewController. view. removeFromSuperview ( )
127
162
transitionContext. completeTransition ( !transitionContext. transitionWasCancelled)
163
+ }
128
164
}
129
165
}
130
166
}
0 commit comments