Skip to content

Commit 783700a

Browse files
committed
Reflected reviews
1 parent 3fbaece commit 783700a

File tree

5 files changed

+104
-38
lines changed

5 files changed

+104
-38
lines changed

Example/AloeStackViewExample/Views/CustomAnimatingLabel.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ import UIKit
1717
import AloeStackView
1818

1919
public class CustomAnimatingLabel: UILabel, CustomAnimating {
20-
21-
public func willAnimate(with coordinator: AnimationCoordinator) {
22-
let isInsertState = coordinator.state == .insert
23-
transform = isInsertState ? CGAffineTransform(translationX: -100, y: 0) : .identity
24-
alpha = isInsertState ? 0 : 1
25-
26-
coordinator.animate(alongsideAnimation: {
27-
self.transform = isInsertState ? .identity : CGAffineTransform(translationX: -100, y: 0)
28-
self.alpha = isInsertState ? 1 : 0
29-
})
30-
}
20+
21+
public func animateInsert() {
22+
transform = .identity
23+
alpha = 1
24+
}
25+
26+
public func insertAnimationWillBegin() {
27+
transform = CGAffineTransform(translationX: -100, y: 0)
28+
alpha = 0
29+
}
30+
31+
public func animateRemove() {
32+
transform = CGAffineTransform(translationX: -100, y: 0)
33+
alpha = 0
34+
}
3135

3236
}

Sources/AloeStackView/AloeStackView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ open class AloeStackView: UIScrollView {
189189
guard let cell = row.superview as? StackViewCell, cell.isHidden != isHidden else { return }
190190

191191
if animated {
192-
let state: State = isHidden ? .delete : .insert
192+
let state: State = isHidden ? .remove : .insert
193193
let coordinator = AnimationCoordinator(target: cell, state: state, animations: {
194194
cell.isHidden = isHidden
195195
cell.layoutIfNeeded()
@@ -502,7 +502,7 @@ open class AloeStackView: UIScrollView {
502502
}
503503

504504
if animated {
505-
let coordinator = AnimationCoordinator(target: cell, state: .delete, animations: {
505+
let coordinator = AnimationCoordinator(target: cell, state: .remove, animations: {
506506
cell.isHidden = true
507507
}, completion: completion)
508508
coordinator.startAnimation()

Sources/AloeStackView/Animation/AnimationCoordinator.swift

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import UIKit
1717

1818
/**
19-
* An object that animates when the Row is inserted and deleted from the AloeStackView.
19+
* An object that animates when the Row is inserted and removed from the AloeStackView.
2020
*/
21-
public class AnimationCoordinator {
21+
internal class AnimationCoordinator {
2222

2323
// MARK: Internal
2424

@@ -30,34 +30,52 @@ public class AnimationCoordinator {
3030
}
3131

3232
internal func startAnimation() {
33-
(target.contentView as? CustomAnimating)?.willAnimate(with: self)
34-
UIView.animate(withDuration: AnimationCoordinator.defaultAnimationDuration, animations: {
33+
let config = target.contentView as? CustomAnimating
34+
35+
willBeginAnimation?()
36+
37+
UIView.animate(withDuration: AnimationCoordinator.defaultAnimationDuration,
38+
delay: 0,
39+
usingSpringWithDamping: config?.springDamping ?? 1,
40+
initialSpringVelocity: 0,
41+
options: [.curveEaseInOut, .allowUserInteraction, .beginFromCurrentState],
42+
animations: {
43+
self.animate?()
3544
self.animations()
36-
self.alongsideAnimation?()
37-
}) { success in
38-
self.completion?(success)
39-
self.alongsideCompletion?(success)
45+
}) { finished in
46+
self.animationDidEnd?(finished)
47+
self.completion?(finished)
4048
}
4149
}
4250

43-
// MARK: Public
44-
45-
public let state: State
46-
47-
/// AloeStackView gives you the opportunity to change state values ​​by taking over the animation behavior when Row is added and removed.
48-
public func animate(alongsideAnimation animation: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {
49-
alongsideAnimation = animation
50-
alongsideCompletion = completion
51-
}
5251

5352
// MARK: Private
5453

5554
private let target: StackViewCell
55+
private let state: State
5656
private let animations: () -> Void
5757
private let completion: ((Bool) -> Void)?
5858

59-
private var alongsideAnimation: (() -> Void)?
60-
private var alongsideCompletion: ((Bool) -> Void)?
59+
private lazy var willBeginAnimation: (() -> Void)? = {
60+
switch state {
61+
case .insert: return (target.contentView as? CustomAnimating)?.insertAnimationWillBegin
62+
case .remove: return (target.contentView as? CustomAnimating)?.removeAnimationWillBegin
63+
}
64+
}()
65+
66+
private lazy var animate: (() -> Void)? = {
67+
switch state {
68+
case .insert: return (target.contentView as? CustomAnimating)?.animateInsert
69+
case .remove: return (target.contentView as? CustomAnimating)?.animateRemove
70+
}
71+
}()
72+
73+
private lazy var animationDidEnd: ((Bool) -> Void)? = {
74+
switch state {
75+
case .insert: return (target.contentView as? CustomAnimating)?.insertAnimationDidEnd
76+
case .remove: return (target.contentView as? CustomAnimating)?.removeAnimationDidEnd
77+
}
78+
}()
6179

6280
private static let defaultAnimationDuration: TimeInterval = 0.3
6381

Sources/AloeStackView/Animation/State.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
// limitations under the License.
1515

1616
/**
17-
* Indicates that the row is added and removed from the AloeStackView.
17+
* Indicates that the row is inserted and removed from the AloeStackView.
1818
*/
19-
public enum State {
19+
internal enum State {
2020

2121
case insert
2222

23-
case delete
23+
case remove
2424

2525
}

Sources/AloeStackView/Protocols/CustomAnimating.swift

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,55 @@
1717
* The rows in `AloeStackView` help Custom Animation to work.
1818
*
1919
* Rows that are added to an `AloeStackView` can conform to this protocol to have their
20-
* You can freely change the state animation of each contentView when a row is inserted or deleted.
20+
* You can freely change the state animation of each contentView when a row is inserted or removed.
2121
*/
22+
23+
import UIKit
24+
2225
public protocol CustomAnimating {
2326

24-
/// Invoked when animated is true when inserting or deleting.
25-
func willAnimate(with coordinator: AnimationCoordinator)
27+
/// The springDamping value used to determine the amount of 'bounce'
28+
/// seen when animationing to insert or remove state
29+
///
30+
/// Default Value is 0.8.
31+
var springDamping: CGFloat { get }
32+
33+
/// Called before insert animation is activated.
34+
func insertAnimationWillBegin()
35+
36+
/// Called when insert animation is working.
37+
func animateInsert()
38+
39+
/// Called after insert animation.
40+
func insertAnimationDidEnd(_ finished: Bool)
41+
42+
/// Called before remove animation is activated.
43+
func removeAnimationWillBegin()
44+
45+
/// Called when remove animation is working.
46+
func animateRemove()
47+
48+
/// Called after remove animation.
49+
func removeAnimationDidEnd(_ finished: Bool)
50+
51+
}
52+
53+
public extension CustomAnimating where Self: UIView {
54+
55+
var springDamping: CGFloat {
56+
return 0.8
57+
}
58+
59+
func animateInsert() { }
60+
61+
func insertAnimationWillBegin() { }
62+
63+
func insertAnimationDidEnd(_ finished: Bool) { }
64+
65+
func animateRemove() { }
66+
67+
func removeAnimationWillBegin() { }
68+
69+
func removeAnimationDidEnd(_ finished: Bool) { }
2670

2771
}

0 commit comments

Comments
 (0)