Skip to content

Commit

Permalink
Add a configuration to disable the animation when the drag ends (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andr3a88 authored Oct 22, 2024
1 parent a970b4c commit 1162887
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Sources/DiscreteSlider/View/DiscreteSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public struct DiscreteSlider<Option: Equatable>: View {
private let handle: AnySliderHandle
private let label: AnySliderLabel<Option>?
private let tickDisplayGuide: TickDisplayGuide
private let animated: Bool
private var onItemPreselected: ((Option) -> Void)?

private var sliderHeight: CGFloat {
Expand All @@ -52,6 +53,7 @@ public struct DiscreteSlider<Option: Equatable>: View {
/// - options: Options that is used as a data source for the slider.
/// - track: Customized slider's track.
/// - tick: Customized slider's tick.
/// - animated: Enable animation of the slider.
/// - handle: Customized slider's handle.
/// - label: Customized slider's label.
/// - selectedItem: Binding to the property that will store the selected item.
Expand All @@ -62,6 +64,7 @@ public struct DiscreteSlider<Option: Equatable>: View {
handle: Handle,
label: Label,
tickDisplayGuide: TickDisplayGuide = .alwaysPresent,
animated: Bool = true,
selectedItem: Binding<Option>,
onItemPreselected: ((Option) -> Void)? = nil
) where Label.Option == Option {
Expand All @@ -70,6 +73,7 @@ public struct DiscreteSlider<Option: Equatable>: View {
self.handle = .init(handle: handle)
self.label = AnySliderLabel<Option>(label: label)
self.tickDisplayGuide = tickDisplayGuide
self.animated = animated
self.options = options
self._selectedItem = selectedItem
self.onItemPreselected = onItemPreselected
Expand All @@ -82,13 +86,15 @@ public struct DiscreteSlider<Option: Equatable>: View {
/// - options: Options that is used as a data source for the slider.
/// - track: Customized slider's track.
/// - handle: Customized slider's handle.
/// - animated: Enable animation of the slider.
/// - selectedItem: Binding to the property that will store the selected item.
public init<Track: SliderTrack, Tick: SliderTick, Handle: SliderHandle>(
options: [Option],
track: Track = DefaultSliderTrack(),
tick: Tick? = DefaultSliderTick(),
handle: Handle = DefaultSliderHandle(),
tickDisplayGuide: TickDisplayGuide = .alwaysPresent,
animated: Bool = true,
selectedItem: Binding<Option>,
onItemPreselected: ((Option) -> Void)? = nil
) {
Expand All @@ -102,6 +108,7 @@ public struct DiscreteSlider<Option: Equatable>: View {
self.label = nil
self.options = options
self.tickDisplayGuide = tickDisplayGuide
self.animated = animated
self._selectedItem = selectedItem
self.onItemPreselected = onItemPreselected
}
Expand All @@ -121,6 +128,7 @@ public struct DiscreteSlider<Option: Equatable>: View {
tick: tick,
handle: handle,
tickDisplayGuide: tickDisplayGuide,
animated: animated,
selectedItem: $selectedItem,
onItemPreselected: onItemPreselected
)
Expand Down
9 changes: 6 additions & 3 deletions Sources/DiscreteSlider/View/SliderControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct SliderControl<Option: Equatable>: View {
private let tick: AnySliderTick?
private let handle: AnySliderHandle
private let tickDisplayGuide: TickDisplayGuide
private let animated: Bool

private let step: CGFloat
@Binding private var selectedItem: Option
Expand Down Expand Up @@ -68,6 +69,7 @@ struct SliderControl<Option: Equatable>: View {
tick: Tick? = DefaultSliderTick(),
handle: Handle = DefaultSliderHandle(),
tickDisplayGuide: TickDisplayGuide = .alwaysPresent,
animated: Bool = true,
selectedItem: Binding<Option>,
onItemPreselected: ((Option) -> Void)? = nil
) {
Expand All @@ -80,6 +82,7 @@ struct SliderControl<Option: Equatable>: View {
}
self.options = options
self.tickDisplayGuide = tickDisplayGuide
self.animated = animated
self._selectedItem = selectedItem

if options.count > 1 {
Expand Down Expand Up @@ -121,7 +124,7 @@ struct SliderControl<Option: Equatable>: View {
updateHandleOffset(width: width, animated: false)
}
.onChange(of: selectedItem) { _ in
updateHandleOffset(width: width, animated: true)
updateHandleOffset(width: width, animated: animated)
}
.onChange(of: width) { newValue in
updateHandleOffset(width: newValue, animated: false)
Expand Down Expand Up @@ -167,7 +170,7 @@ struct SliderControl<Option: Equatable>: View {

private func dragEnded(on location: CGFloat, width: CGFloat) {
if step == 0, let item = options.first {
setSelectedItem(item, animated: true)
setSelectedItem(item, animated: animated)
return
}

Expand All @@ -176,7 +179,7 @@ struct SliderControl<Option: Equatable>: View {
let percentage = max(0, min(location / lineWidth, 1))
let page = round(percentage / step)

setSelectedItem(options[Int(page)], animated: true)
setSelectedItem(options[Int(page)], animated: animated)
}

private func setSelectedItem(_ item: Option, animated: Bool) {
Expand Down

0 comments on commit 1162887

Please sign in to comment.