Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make slider movement smoother #1

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ struct Element: GraphRangeElement {
}

struct ContentView: View {
let data: [Element] = [
.init(x: 10, y: 10),
.init(x: 20, y: 20),
.init(x: 30, y: 30),
.init(x: 40, y: 40),
.init(x: 50, y: 50),
.init(x: 60, y: 60),
.init(x: 70, y: 70),
]
let data: [Element] = (1...50).map { .init(x: $0 * 10, y: $0 * 10) }

@State var selectedData = [Element]()

Expand Down
7 changes: 7 additions & 0 deletions Sources/GraphRangeSlider/Extensions/Array+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension Array where Element: Comparable {
func argmin() -> Index? {
indices.min(by: { self[$0] < self[$1] })
}
}
2 changes: 1 addition & 1 deletion Sources/GraphRangeSlider/GraphRangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct GraphRangeSlider<Data, ID>: View where Data: RandomAccessCollectio
@Binding private var selectedData: Data
@State private var leftCurrentIndex = 0
@State private var rightCurrentIndex = 0
@State private var positions = [CGFloat]()
@State private var positions = ContiguousArray<CGFloat>()
@Environment(\.graphBarWidth) private var graphBarWidth: CGFloat
@Environment(\.activeColor) private var activeColor: Color
@Environment(\.inactiveColor) private var inactiveColor: Color
Expand Down
2 changes: 1 addition & 1 deletion Sources/GraphRangeSlider/Slider.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

struct Slider: View {
let positions: [CGFloat]
let positions: ContiguousArray<CGFloat>
@Binding var leftCurrentIndex: Int
@Binding var rightCurrentIndex: Int
@State private var viewSize = CGSize.zero
Expand Down
17 changes: 5 additions & 12 deletions Sources/GraphRangeSlider/SliderToggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct SliderToggle: View {
case left, right
}

let togglePositions: [CGFloat]
let togglePositions: ContiguousArray<CGFloat>
let limitIndex: Int
@Binding var currentIndex: Int
@Environment(\.activeColor) private var activeColor: Color
Expand All @@ -31,23 +31,16 @@ struct SliderToggle: View {
.gesture(
DragGesture()
.onChanged { value in
let nextIndex = min(currentIndex + 1, togglePositions.endIndex - 1)
let previousIndex = max(currentIndex - 1, 0)

switch position {
case .left:
let locationX = value.location.x - toggleRadius
if togglePositions[nextIndex] < locationX {
currentIndex = min(nextIndex, limitIndex - 1)
} else if locationX < togglePositions[previousIndex] {
currentIndex = previousIndex
if let index = togglePositions.map { abs($0 - locationX) }.argmin() {
currentIndex = min(index, limitIndex - 1)
}
case .right:
let locationX = value.location.x + toggleRadius * 3
if togglePositions[nextIndex] < locationX {
currentIndex = nextIndex
} else if locationX < togglePositions[previousIndex] {
currentIndex = max(previousIndex, limitIndex + 1)
if let index = togglePositions.map { abs($0 - locationX) }.argmin() {
currentIndex = max(index, limitIndex + 1)
}
}
}
Expand Down