From 71900aa8d6ce6005583381d57853a84a3f74317a Mon Sep 17 00:00:00 2001 From: Steven Huang <140762048+shuang886@users.noreply.github.com> Date: Sat, 16 Sep 2023 15:30:16 -0700 Subject: [PATCH] steps should be discrete offsets from lowerBound - i.e., lowerBound = 3.5, step = 0.2, values should be 3.7, 3.9, etc. --- Sources/Sliders/Base/LinearValueMath.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Sliders/Base/LinearValueMath.swift b/Sources/Sliders/Base/LinearValueMath.swift index f8efe56..a63193f 100644 --- a/Sources/Sliders/Base/LinearValueMath.swift +++ b/Sources/Sliders/Base/LinearValueMath.swift @@ -16,8 +16,8 @@ import SwiftUI /// Example: For relative value 0.5 in range 2.0..4.0 produces 3.0 @inlinable func valueFrom(distance: CGFloat, availableDistance: CGFloat, bounds: ClosedRange = 0.0...1.0, step: CGFloat = 0.001, leadingOffset: CGFloat = 0, trailingOffset: CGFloat = 0) -> CGFloat { let relativeValue = (distance - leadingOffset) / (availableDistance - (leadingOffset + trailingOffset)) - let newValue = bounds.lowerBound + (relativeValue * (bounds.upperBound - bounds.lowerBound)) - let steppedNewValue = (round(newValue / step) * step) + let newValue = relativeValue * (bounds.upperBound - bounds.lowerBound) + let steppedNewValue = bounds.lowerBound + round(newValue / step) * step let validatedValue = min(bounds.upperBound, max(bounds.lowerBound, steppedNewValue)) return validatedValue }