forked from SwipeCellKit/SwipeCellKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwipeActionTransitioning.swift
106 lines (84 loc) · 3.84 KB
/
SwipeActionTransitioning.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// SwipeActionTransitioning.swift
//
// Created by Jeremy Koch
// Copyright © 2017 Jeremy Koch. All rights reserved.
//
import UIKit
/**
Adopt the `SwipeActionTransitioning` protocol in objects that implement custom appearance of actions during transition.
*/
public protocol SwipeActionTransitioning {
/**
Tells the delegate that transition change has occured.
*/
func didTransition(with context: SwipeActionTransitioningContext) -> Void
}
/**
The `SwipeActionTransitioningContext` type provides information relevant to a specific action as transitioning occurs.
*/
public struct SwipeActionTransitioningContext {
/// The unique action identifier.
public let actionIdentifier: String?
/// The button that is changing.
public let button: UIButton
/// The old visibility percentage between 0.0 and 1.0.
public let newPercentVisible: CGFloat
/// The new visibility percentage between 0.0 and 1.0.
public let oldPercentVisible: CGFloat
internal let wrapperView: UIView
internal init(actionIdentifier: String?, button: UIButton, newPercentVisible: CGFloat, oldPercentVisible: CGFloat, wrapperView: UIView) {
self.actionIdentifier = actionIdentifier
self.button = button
self.newPercentVisible = newPercentVisible
self.oldPercentVisible = oldPercentVisible
self.wrapperView = wrapperView
}
/// Sets the background color behind the action button.
///
/// - parameter color: The background color.
public func setBackgroundColor(_ color: UIColor?) {
wrapperView.backgroundColor = color
}
}
/**
A scale transition object drives the custom appearance of actions during transition.
As button's percentage visibility crosses the `threshold`, the `ScaleTransition` object will animate from `initialScale` to `identity`. The default settings provide a "pop-like" effect as the buttons are exposed more than 50%.
*/
public struct ScaleTransition: SwipeActionTransitioning {
/// Returns a `ScaleTransition` instance with default transition options.
public static var `default`: ScaleTransition { return ScaleTransition() }
/// The duration of the animation.
public let duration: Double
/// The initial scale factor used before the action button percent visible is greater than the threshold.
public let initialScale: CGFloat
/// The percent visible threshold that triggers the scaling animation.
public let threshold: CGFloat
/**
Contructs a new `ScaleTransition` instance.
- parameter duration: The duration of the animation.
- parameter initialScale: The initial scale factor used before the action button percent visible is greater than the threshold.
- parameter threshold: The percent visible threshold that triggers the scaling animation.
- returns: The new `ScaleTransition` instance.
*/
public init(duration: Double = 0.15, initialScale: CGFloat = 0.8, threshold: CGFloat = 0.5) {
self.duration = duration
self.initialScale = initialScale
self.threshold = threshold
}
/// :nodoc:
public func didTransition(with context: SwipeActionTransitioningContext) -> Void {
if context.oldPercentVisible == 0 {
context.button.transform = .init(scaleX: initialScale, y: initialScale)
}
if context.oldPercentVisible < threshold && context.newPercentVisible >= threshold {
UIView.animate(withDuration: duration) {
context.button.transform = .identity
}
} else if context.oldPercentVisible >= threshold && context.newPercentVisible < threshold {
UIView.animate(withDuration: duration) {
context.button.transform = .init(scaleX: self.initialScale, y: self.initialScale)
}
}
}
}