Skip to content

Commit 5a53541

Browse files
committed
Add animation progressDidChange. Update demo. Fix #23
1 parent f589856 commit 5a53541

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

Demo/Sources/Demo/SystemDemo.swift

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct CompactSliderSystemDemo: View {
1111

1212
@State private var playerTime: TimeInterval = 10
1313
@State private var isPlaying = false
14+
@State private var wasPlaying = false
1415
@State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
1516

1617
var body: some View {
@@ -59,6 +60,18 @@ struct CompactSliderSystemDemo: View {
5960
.systemSliderStyle(handleStyle: .hidden())
6061
.compactSliderOptionsByAdding(.expandOnFocus(minScale: 0.5))
6162
.compactSliderAnimation(.bouncy, when: .dragging, .hovering)
63+
.compactSliderAnimation(isPlaying ? .linear(duration: 1) : nil, when: .progressDidChange)
64+
.compactSliderOnChange { configuration in
65+
if configuration.focusState.isDragging {
66+
Task {
67+
isPlaying = false
68+
}
69+
} else if wasPlaying {
70+
Task {
71+
isPlaying = true
72+
}
73+
}
74+
}
6275

6376
if #available(macOS 13.0, iOS 16.0, *) {
6477
HStack(alignment: .top) {
@@ -67,8 +80,11 @@ struct CompactSliderSystemDemo: View {
6780
.foregroundStyle(.secondary)
6881
Spacer()
6982

70-
Button(action: { isPlaying.toggle() }) {
71-
Image(systemName: isPlaying ? "pause.fill" : "play.fill")
83+
Button(action: {
84+
isPlaying.toggle()
85+
wasPlaying = isPlaying
86+
}) {
87+
Image(systemName: wasPlaying ? "pause.fill" : "play.fill")
7288
}
7389
.buttonStyle(BorderlessButtonStyle())
7490
.font(.title)
@@ -82,20 +98,20 @@ struct CompactSliderSystemDemo: View {
8298
.font(.footnote)
8399
.monospacedDigit()
84100
.padding(.horizontal, 6)
101+
.animation(.default, value: playerTime)
85102
}
86103
}
87104
.onReceive(timer) { _ in
88105
guard isPlaying else { return }
89106

90-
withAnimation {
91-
if playerTime >= 59 {
92-
isPlaying = false
93-
playerTime = 60
94-
return
95-
}
96-
97-
playerTime += 1
107+
if playerTime >= 59 {
108+
isPlaying = false
109+
wasPlaying = isPlaying
110+
playerTime = 60
111+
return
98112
}
113+
114+
playerTime += 1
99115
}
100116
}
101117
}

Sources/CompactSlider/CompactSliderAnimations.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum CompactSliderAnimationEvent {
1010
case hovering
1111
case dragging
1212
case tapping
13+
case progressDidChange
1314
case wheelScrolling
1415
}
1516

Sources/CompactSlider/Components/Progress/ProgressContainerView.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SwiftUI
77

88
struct ProgressContainerView<V: View>: View {
99
@Environment(\.compactSliderStyleConfiguration) var configuration
10+
@Environment(\.compactSliderAnimations) var animations
1011
@Environment(\.handleStyle) var environmentHandleStyle
1112
var handleStyle: HandleStyle { environmentHandleStyle.byType(configuration.type) }
1213
let progressView: (CompactSliderStyleConfiguration) -> V
@@ -32,5 +33,17 @@ struct ProgressContainerView<V: View>: View {
3233
.offset(x: offset.x, y: offset.y)
3334
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: alignment)
3435
.allowsHitTesting(false)
36+
.animateProgress(animations[.progressDidChange], progress: configuration.progress)
37+
}
38+
}
39+
40+
private extension View {
41+
@ViewBuilder
42+
func animateProgress(_ progressAnimation: Animation?, progress: Progress) -> some View {
43+
if let progressAnimation {
44+
animation(progressAnimation, value: progress.progresses)
45+
} else {
46+
self
47+
}
3548
}
3649
}

0 commit comments

Comments
 (0)