Skip to content

Commit 0ddf554

Browse files
committed
Transfer observer from commonInit to init
1 parent 89cdcdd commit 0ddf554

File tree

1 file changed

+10
-67
lines changed

1 file changed

+10
-67
lines changed

SpringAnimation/Sources/SpringAnimation/SpringAnimation.swift

+10-67
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,18 @@
11
import UIKit
22

3-
4-
/// Animation options
5-
public protocol Springable: AnyObject {
6-
// Animation properties
7-
/// Automatic animation start
8-
var autostart: Bool { get set }
9-
/// Hides the view
10-
var autohide: Bool { get set }
11-
/// Animation name
12-
var animation: String { get set }
13-
/// The of animation
14-
var force: CGFloat { get set }
15-
/// The delay (in seconds) after which the animations begin.
16-
///
17-
/// The default value of this property is 0. When the value is greater than 0, the start of any animations is delayed by the specified amount of time.
18-
var delay: CGFloat { get set }
19-
/// The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them.
20-
var duration: CGFloat { get set }
21-
/// Defines how the spring’s motion should be damped due to the forces of friction.
22-
var damping: CGFloat { get set }
23-
/// The initial velocity of the object attached to the spring.
24-
var velocity: CGFloat { get set }
25-
/// Determines the number of times the animation will repeat.
26-
var repeatCount: Float { get set }
27-
/// The x-coordinate of the point.
28-
var x: CGFloat { get set }
29-
/// The y-coordinate of the point.
30-
var y: CGFloat { get set }
31-
/// A value function scales by the input value along the x-axis. Animations referencing this value transform function must provide a single animation value.
32-
var scaleX: CGFloat { get set }
33-
/// A value function scales by the input value along the y-axis. Animations referencing this value function must provide a single animation value.
34-
var scaleY: CGFloat { get set }
35-
/// Object rotation
36-
var rotate: CGFloat { get set }
37-
///The opacity of the receiver. Animatable.
38-
///
39-
///The value of this property must be in the range 0.0 (transparent) to 1.0 (opaque). Values outside that range are clamped to the minimum or maximum. The default value of this property is 1.0.
40-
var opacity: CGFloat { get set }
41-
var animateFrom: Bool { get set }
42-
/// Animation preset
43-
var curve: String { get set }
44-
45-
// UIView
46-
var layer : CALayer { get }
47-
var transform : CGAffineTransform { get set }
48-
var alpha : CGFloat { get set }
49-
50-
/// Run the animation with the given parameters
51-
func animate()
52-
/// Run next animation after complete current animation
53-
func animateNext(completion: @escaping() -> Void)
54-
}
55-
563
public final class SpringAnimation {
574
private unowned var view: Springable
585
private var shouldAnimateAfterActive = false
596
private var shouldAnimateInLayoutSubviews = true
607

618
init(view: Springable) {
629
self.view = view
63-
commonInit()
10+
NotificationCenter.default.addObserver(
11+
self,
12+
selector: #selector(didBecomeActiveNotification),
13+
name: UIApplication.didBecomeActiveNotification,
14+
object: nil
15+
)
6416
}
6517

6618
public func customAwakeFromNib() {
@@ -86,7 +38,7 @@ public final class SpringAnimation {
8638
public func animate() {
8739
view.animateFrom = true
8840
animationPreset()
89-
setView {}
41+
setView()
9042
}
9143

9244
public func animateNext(completion: @escaping() -> Void) {
@@ -100,7 +52,7 @@ public final class SpringAnimation {
10052
public func animateTo() {
10153
view.animateFrom = false
10254
animationPreset()
103-
setView {}
55+
setView()
10456
}
10557

10658
public func animateToNext(completion: @escaping () -> ()) {
@@ -111,15 +63,6 @@ public final class SpringAnimation {
11163
}
11264
}
11365

114-
private func commonInit() {
115-
NotificationCenter.default.addObserver(
116-
self,
117-
selector: #selector(didBecomeActiveNotification),
118-
name: UIApplication.didBecomeActiveNotification,
119-
object: nil
120-
)
121-
}
122-
12366
@objc private func didBecomeActiveNotification(_ notification: NSNotification) {
12467
if shouldAnimateAfterActive {
12568
view.alpha = 0
@@ -337,7 +280,7 @@ public final class SpringAnimation {
337280
}
338281
}
339282

340-
private func setView(completion: @escaping() -> Void) {
283+
private func setView(completion: (() -> Void)? = nil) {
341284
if view.animateFrom {
342285
let translate = CGAffineTransform(translationX: view.x, y: view.y)
343286
let scale = CGAffineTransform(scaleX: view.scaleX, y: view.scaleY)
@@ -372,7 +315,7 @@ public final class SpringAnimation {
372315
}
373316
},
374317
completion: { [weak self] _ in
375-
completion()
318+
completion?()
376319
self?.resetAll()
377320
}
378321
)

0 commit comments

Comments
 (0)