Skip to content

Commit 50dd96a

Browse files
committed
remove some force unwrap
1 parent 3538f79 commit 50dd96a

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

Sources/Hero2/Base/Transition.swift

+38-35
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open class Transition: NSObject {
1111
public static var animatingTransitionCount: Int = 0
1212
public private(set) var isPresenting: Bool = true
1313
public private(set) var isInteractive = false
14-
public private(set) var animator: UIViewPropertyAnimator?
14+
public private(set) var animator: UIViewPropertyAnimator
1515
public private(set) weak var navigationController: UINavigationController?
1616
public private(set) var transitionContext: UIViewControllerContextTransitioning?
1717
public private(set) var isTransitioning: Bool = false
@@ -30,17 +30,17 @@ open class Transition: NSObject {
3030
public var timingParameters: UITimingCurveProvider
3131

3232
public var isReversed: Bool {
33-
animator?.isReversed ?? false
33+
animator.isReversed
3434
}
3535

3636
open var automaticallyLayoutToView: Bool {
3737
true
3838
}
3939

4040
public var fractionCompleted: CGFloat {
41-
get { animator?.fractionComplete ?? 0 }
41+
get { animator.fractionComplete }
4242
set {
43-
guard let animator = animator, animator.state == .active else { return }
43+
guard animator.state == .active else { return }
4444
animator.fractionComplete = newValue
4545
}
4646
}
@@ -69,14 +69,15 @@ open class Transition: NSObject {
6969
) {
7070
self.duration = duration
7171
self.timingParameters = timingParameters
72+
self.animator = UIViewPropertyAnimator(duration: duration, timingParameters: timingParameters)
7273
super.init()
7374
}
7475

7576
open func beginInteractiveTransition() {
7677
isInteractive = true
7778
isTransitioning = true
7879

79-
animator?.pauseAnimation()
80+
animator.pauseAnimation()
8081
transitionContext?.pauseInteractiveTransition()
8182
}
8283

@@ -95,13 +96,11 @@ open class Transition: NSObject {
9596
transitionContext?.cancelInteractiveTransition()
9697
}
9798

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)
105104
}
106105
}
107106

@@ -184,7 +183,7 @@ extension Transition {
184183

185184
extension Transition: UIViewControllerInteractiveTransitioning {
186185
open func interruptibleAnimator(using _: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating {
187-
animator!
186+
animator
188187
}
189188

190189
open func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
@@ -209,21 +208,26 @@ extension Transition: UIViewControllerAnimatedTransitioning {
209208
}
210209
prepareBlocks.removeAll()
211210

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()
225230
}
226-
toView!.layoutIfNeeded()
227231
}
228232

229233
isAnimating = true
@@ -255,26 +259,26 @@ extension Transition: UIViewControllerAnimatedTransitioning {
255259

256260
if isPresenting {
257261
dismissedState()
258-
animator!.addAnimations(presentedState)
262+
animator.addAnimations(presentedState)
259263
} else {
260264
presentedState()
261-
animator!.addAnimations(dismissedState)
265+
animator.addAnimations(dismissedState)
262266
}
263267

264268
// flush the current transaction before animation start.
265269
// otherwise delay animation on dismiss might not be registered.
266270
CATransaction.flush()
267271

268-
animator!
272+
animator
269273
.addCompletion { [weak self] pos in
270-
container.isUserInteractionEnabled = true
274+
self?.transitionContainer?.isUserInteractionEnabled = true
271275
completion(pos == .end)
272276
self?.completeTransition(finished: pos == .end)
273277
}
274278

275-
animator?.startAnimation()
279+
animator.startAnimation()
276280
if isInteractive {
277-
animator?.pauseAnimation()
281+
animator.pauseAnimation()
278282
}
279283
}
280284

@@ -293,7 +297,7 @@ extension Transition: UIViewControllerAnimatedTransitioning {
293297
duration
294298
}
295299

296-
open func completeTransition(finished: Bool) {
300+
@objc open func completeTransition(finished: Bool) {
297301
if finished {
298302
if !toOverFullScreen {
299303
fromView?.removeFromSuperview()
@@ -321,7 +325,6 @@ extension Transition: UIViewControllerAnimatedTransitioning {
321325
open func animationEnded(_ transitionCompleted: Bool) {
322326
pausedAnimations.removeAll()
323327
transitionContext = nil
324-
animator = nil
325328
navigationController = nil
326329
isTransitioning = false
327330
isInteractive = false

Sources/Hero2/HeroTransition/HeroTransition.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ open class HeroTransition: Transition {
141141
view.isHidden = true
142142
}
143143

144-
let duration = animator!.duration
144+
let duration = animator.duration
145145
let isOverCurrentContext = transitionContext?.presentationStyle == .overFullScreen || transitionContext?.presentationStyle == .overCurrentContext
146146
for view in animatingViews {
147147
let viewContext = contexts[view]!

0 commit comments

Comments
 (0)