Skip to content

Commit 74db724

Browse files
Merge pull request #102 from componentskit/smooth-scale-animation
Make scale animations in buttons and cards smoother
2 parents d54585b + 01f0831 commit 74db724

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

Sources/ComponentsKit/Components/Button/SUButton.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public struct SUButton: View {
99
/// A closure that is triggered when the button is tapped.
1010
public var action: () -> Void
1111

12-
/// A Boolean value indicating whether the button is pressed.
13-
@State public var isPressed: Bool = false
12+
/// A current scale effect value.
13+
@State public var scale: CGFloat = 1.0
1414

1515
// MARK: Initialization
1616

@@ -37,17 +37,15 @@ public struct SUButton: View {
3737
.buttonStyle(CustomButtonStyle(model: self.model))
3838
.simultaneousGesture(DragGesture(minimumDistance: 0.0)
3939
.onChanged { _ in
40-
self.isPressed = true
40+
self.scale = self.model.animationScale.value
4141
}
4242
.onEnded { _ in
43-
self.isPressed = false
43+
self.scale = 1.0
4444
}
4545
)
4646
.disabled(!self.model.isInteractive)
47-
.scaleEffect(
48-
self.isPressed ? self.model.animationScale.value : 1,
49-
anchor: .center
50-
)
47+
.scaleEffect(self.scale, anchor: .center)
48+
.animation(.easeOut(duration: 0.05), value: self.scale)
5149
}
5250

5351
@ViewBuilder

Sources/ComponentsKit/Components/Button/UKButton.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ open class UKButton: FullWidthComponent, UKComponent {
1818
/// A Boolean value indicating whether the button is pressed.
1919
public private(set) var isPressed: Bool = false {
2020
didSet {
21-
self.transform = self.isPressed && self.model.isInteractive
22-
? .init(
23-
scaleX: self.model.animationScale.value,
24-
y: self.model.animationScale.value
25-
)
26-
: .identity
21+
UIView.animate(withDuration: 0.05, delay: 0, options: [.curveEaseOut]) {
22+
self.transform = self.isPressed && self.model.isInteractive
23+
? .init(
24+
scaleX: self.model.animationScale.value,
25+
y: self.model.animationScale.value
26+
)
27+
: .identity
28+
}
2729
}
2830
}
2931

Sources/ComponentsKit/Components/Card/SUCard.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public struct SUCard<Content: View>: View {
1919
/// A closure that is triggered when the card is tapped.
2020
public var onTap: () -> Void
2121

22-
/// A Boolean value indicating whether the card is pressed.
23-
@State public var isPressed: Bool = false
22+
/// A current scale effect value.
23+
@State public var scale: CGFloat = 1.0
2424

2525
@ViewBuilder private let content: () -> Content
2626
@State private var contentSize: CGSize = .zero
@@ -66,16 +66,14 @@ public struct SUCard<Content: View>: View {
6666
.simultaneousGesture(
6767
DragGesture(minimumDistance: 0.0)
6868
.onChanged { _ in
69-
self.isPressed = true
69+
self.scale = self.model.animationScale.value
7070
}
7171
.onEnded { _ in
72-
self.isPressed = false
72+
self.scale = 1.0
7373
},
7474
isEnabled: self.model.isTappable
7575
)
76-
.scaleEffect(
77-
self.isPressed ? self.model.animationScale.value : 1,
78-
anchor: .center
79-
)
76+
.scaleEffect(self.scale, anchor: .center)
77+
.animation(.easeOut(duration: 0.05), value: self.scale)
8078
}
8179
}

Sources/ComponentsKit/Components/Card/UKCard.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ open class UKCard<Content: UIView>: UIView, UKComponent {
2929
/// A Boolean value indicating whether the button is pressed.
3030
public private(set) var isPressed: Bool = false {
3131
didSet {
32-
self.transform = self.isPressed
33-
? .init(
34-
scaleX: self.model.animationScale.value,
35-
y: self.model.animationScale.value
36-
)
37-
: .identity
32+
UIView.animate(withDuration: 0.05, delay: 0, options: [.curveEaseOut]) {
33+
self.transform = self.isPressed
34+
? .init(
35+
scaleX: self.model.animationScale.value,
36+
y: self.model.animationScale.value
37+
)
38+
: .identity
39+
}
3840
}
3941
}
4042

0 commit comments

Comments
 (0)