@@ -11,7 +11,7 @@ open class Transition: NSObject {
11
11
public static var animatingTransitionCount : Int = 0
12
12
public private( set) var isPresenting : Bool = true
13
13
public private( set) var isInteractive = false
14
- public private( set) var animator : UIViewPropertyAnimator ?
14
+ public private( set) var animator : UIViewPropertyAnimator
15
15
public private( set) weak var navigationController : UINavigationController ?
16
16
public private( set) var transitionContext : UIViewControllerContextTransitioning ?
17
17
public private( set) var isTransitioning : Bool = false
@@ -30,17 +30,17 @@ open class Transition: NSObject {
30
30
public var timingParameters : UITimingCurveProvider
31
31
32
32
public var isReversed : Bool {
33
- animator? . isReversed ?? false
33
+ animator. isReversed
34
34
}
35
35
36
36
open var automaticallyLayoutToView : Bool {
37
37
true
38
38
}
39
39
40
40
public var fractionCompleted : CGFloat {
41
- get { animator? . fractionComplete ?? 0 }
41
+ get { animator. fractionComplete }
42
42
set {
43
- guard let animator = animator , animator. state == . active else { return }
43
+ guard animator. state == . active else { return }
44
44
animator. fractionComplete = newValue
45
45
}
46
46
}
@@ -69,14 +69,15 @@ open class Transition: NSObject {
69
69
) {
70
70
self . duration = duration
71
71
self . timingParameters = timingParameters
72
+ self . animator = UIViewPropertyAnimator ( duration: duration, timingParameters: timingParameters)
72
73
super. init ( )
73
74
}
74
75
75
76
open func beginInteractiveTransition( ) {
76
77
isInteractive = true
77
78
isTransitioning = true
78
79
79
- animator? . pauseAnimation ( )
80
+ animator. pauseAnimation ( )
80
81
transitionContext? . pauseInteractiveTransition ( )
81
82
}
82
83
@@ -95,13 +96,11 @@ open class Transition: NSObject {
95
96
transitionContext? . cancelInteractiveTransition ( )
96
97
}
97
98
98
- if let animator = animator {
99
- animator. isReversed = !shouldFinish
100
- if animator. state == . inactive {
101
- animator. startAnimation ( )
102
- } else {
103
- animator. continueAnimation ( withTimingParameters: nil , durationFactor: 1 )
104
- }
99
+ animator. isReversed = !shouldFinish
100
+ if animator. state == . inactive {
101
+ animator. startAnimation ( )
102
+ } else {
103
+ animator. continueAnimation ( withTimingParameters: nil , durationFactor: 1 )
105
104
}
106
105
}
107
106
@@ -184,7 +183,7 @@ extension Transition {
184
183
185
184
extension Transition : UIViewControllerInteractiveTransitioning {
186
185
open func interruptibleAnimator( using _: UIViewControllerContextTransitioning ) -> UIViewImplicitlyAnimating {
187
- animator!
186
+ animator
188
187
}
189
188
190
189
open func startInteractiveTransition( _ transitionContext: UIViewControllerContextTransitioning ) {
@@ -209,21 +208,26 @@ extension Transition: UIViewControllerAnimatedTransitioning {
209
208
}
210
209
prepareBlocks. removeAll ( )
211
210
212
- let container = transitionContainer!
213
- if !isUserInteractionEnabled {
214
- container. isUserInteractionEnabled = isUserInteractionEnabled
215
- }
216
- container. addSubview ( backgroundView!)
217
- container. addSubview ( foregroundView!)
218
- if automaticallyLayoutToView {
219
- if !isPresenting {
220
- toViewController? . presentationController? . containerViewWillLayoutSubviews ( )
221
- container. layoutSubviews ( )
222
- toViewController? . presentationController? . containerViewDidLayoutSubviews ( )
223
- } else {
224
- toView!. frameWithoutTransform = container. frame
211
+ if let container = transitionContainer {
212
+ if !isUserInteractionEnabled {
213
+ container. isUserInteractionEnabled = isUserInteractionEnabled
214
+ }
215
+ if let backgroundView {
216
+ container. addSubview ( backgroundView)
217
+ }
218
+ if let foregroundView {
219
+ container. addSubview ( foregroundView)
220
+ }
221
+ if automaticallyLayoutToView {
222
+ if !isPresenting {
223
+ toViewController? . presentationController? . containerViewWillLayoutSubviews ( )
224
+ container. layoutSubviews ( )
225
+ toViewController? . presentationController? . containerViewDidLayoutSubviews ( )
226
+ } else {
227
+ toView? . frameWithoutTransform = container. frame
228
+ }
229
+ toView? . layoutIfNeeded ( )
225
230
}
226
- toView!. layoutIfNeeded ( )
227
231
}
228
232
229
233
isAnimating = true
@@ -255,26 +259,26 @@ extension Transition: UIViewControllerAnimatedTransitioning {
255
259
256
260
if isPresenting {
257
261
dismissedState ( )
258
- animator! . addAnimations ( presentedState)
262
+ animator. addAnimations ( presentedState)
259
263
} else {
260
264
presentedState ( )
261
- animator! . addAnimations ( dismissedState)
265
+ animator. addAnimations ( dismissedState)
262
266
}
263
267
264
268
// flush the current transaction before animation start.
265
269
// otherwise delay animation on dismiss might not be registered.
266
270
CATransaction . flush ( )
267
271
268
- animator!
272
+ animator
269
273
. addCompletion { [ weak self] pos in
270
- container . isUserInteractionEnabled = true
274
+ self ? . transitionContainer ? . isUserInteractionEnabled = true
271
275
completion ( pos == . end)
272
276
self ? . completeTransition ( finished: pos == . end)
273
277
}
274
278
275
- animator? . startAnimation ( )
279
+ animator. startAnimation ( )
276
280
if isInteractive {
277
- animator? . pauseAnimation ( )
281
+ animator. pauseAnimation ( )
278
282
}
279
283
}
280
284
@@ -293,7 +297,7 @@ extension Transition: UIViewControllerAnimatedTransitioning {
293
297
duration
294
298
}
295
299
296
- open func completeTransition( finished: Bool ) {
300
+ @ objc open func completeTransition( finished: Bool ) {
297
301
if finished {
298
302
if !toOverFullScreen {
299
303
fromView? . removeFromSuperview ( )
@@ -321,7 +325,6 @@ extension Transition: UIViewControllerAnimatedTransitioning {
321
325
open func animationEnded( _ transitionCompleted: Bool ) {
322
326
pausedAnimations. removeAll ( )
323
327
transitionContext = nil
324
- animator = nil
325
328
navigationController = nil
326
329
isTransitioning = false
327
330
isInteractive = false
0 commit comments