Skip to content

Commit ebf93bd

Browse files
author
Kieran Brown
committed
Fixed issues with new SwiftUI layout. The LSlider and Trackpad had improper positioning after updated to the latest Xcode Beta. I manually set the position of the thumb to be centered in the containing rectangle at 0 offset.
1 parent cccc82d commit ebf93bd

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

Sources/Sliders/LSlider.swift

+49-35
Original file line numberDiff line numberDiff line change
@@ -171,28 +171,38 @@ public struct LSlider: View {
171171
public var range: ClosedRange<Double> = 0...1
172172
public var angle: Angle = .zero
173173
public var isDisabled: Bool = false
174-
174+
175175
public init(_ value: Binding<Double>, range: ClosedRange<Double>, angle: Angle, isDisabled: Bool = false) {
176176
self._value = value
177177
self.range = range
178178
self.angle = angle
179179
self.isDisabled = isDisabled
180180
}
181-
181+
182+
public init(_ value: Binding<Double>, range: ClosedRange<Double>, isDisabled: Bool = false) {
183+
self._value = value
184+
self.range = range
185+
self.isDisabled = isDisabled
186+
}
187+
188+
public init(_ value: Binding<Double>, angle: Angle, isDisabled: Bool = false) {
189+
self._value = value
190+
self.angle = angle
191+
self.isDisabled = isDisabled
192+
}
193+
182194
public init(_ value: Binding<Double>) {
183195
self._value = value
184-
185196
}
186-
187-
197+
188198
// MARK: Calculations
189199
// uses an arbitrarily large number to gesture a line segment that is guarenteed to intersect with the
190200
// bounding box, then finds those points of intersection to be used as the start and end points of the slider
191201
private func calculateEndPoints(_ proxy: GeometryProxy) -> (start: CGPoint, end: CGPoint) {
192202
let w = proxy.size.width
193203
let h = proxy.size.height
194204
let big: CGFloat = 50000000
195-
205+
196206
let x1 = w/2 + big*CGFloat(cos(self.angle.radians))
197207
let y1 = h/2 + big*CGFloat(sin(self.angle.radians))
198208
let x2 = w/2 - big*CGFloat(cos(self.angle.radians))
@@ -201,7 +211,7 @@ public struct LSlider: View {
201211
if points.count < 2 {
202212
return (.zero, .zero)
203213
}
204-
214+
205215
return (points[0], points[1])
206216
}
207217
private func thumbOffset(_ proxy: GeometryProxy) -> CGSize {
@@ -211,7 +221,7 @@ public struct LSlider: View {
211221
let y = (1-value)*Double(ends.start.y) + value*Double(ends.end.y) - Double(proxy.size.height/2)
212222
return CGSize(width: x, height: y)
213223
}
214-
224+
215225
private var configuration: LSliderConfiguration {
216226
.init(isDisabled: isDisabled,
217227
isActive: isActive,
@@ -221,7 +231,7 @@ public struct LSlider: View {
221231
min: range.lowerBound,
222232
max: range.upperBound)
223233
}
224-
234+
225235
// MARK: Haptics
226236
private func impactOccured() {
227237
#if os(macOS)
@@ -230,6 +240,7 @@ public struct LSlider: View {
230240
generator.impactOccurred()
231241
#endif
232242
}
243+
233244
private func impactHandler(_ parameterAtLimit: Bool) {
234245
if parameterAtLimit {
235246
if !atLimit {
@@ -240,36 +251,39 @@ public struct LSlider: View {
240251
atLimit = false
241252
}
242253
}
254+
255+
// MARK: - Gesture
256+
private func makeGesture(_ proxy: GeometryProxy) -> some Gesture {
257+
DragGesture(minimumDistance: 10, coordinateSpace: .named(self.space))
258+
.onChanged({ drag in
259+
let ends = self.calculateEndPoints(proxy)
260+
let parameter = Double(calculateParameter(ends.start, ends.end, drag.location))
261+
self.impactHandler(parameter == 1 || parameter == 0)
262+
self.value = (self.range.upperBound-self.range.lowerBound)*parameter + self.range.lowerBound
263+
self.isActive = true
264+
})
265+
.onEnded({ (drag) in
266+
let ends = self.calculateEndPoints(proxy)
267+
let parameter = Double(calculateParameter(ends.start, ends.end, drag.location))
268+
self.impactHandler(parameter == 1 || parameter == 0)
269+
self.value = (self.range.upperBound-self.range.lowerBound)*parameter + self.range.lowerBound
270+
self.isActive = false
271+
})
272+
}
273+
243274
// MARK: View
244275
public var body: some View {
245276
ZStack {
246277
style.makeTrack(configuration: configuration)
247-
.overlay(
248-
GeometryReader { proxy in
249-
ZStack {
250-
self.style.makeThumb(configuration: self.configuration)
251-
.offset(self.thumbOffset(proxy))
252-
.gesture(DragGesture(minimumDistance: 10, coordinateSpace: .named(self.space))
253-
.onChanged({ drag in
254-
let ends = self.calculateEndPoints(proxy)
255-
let parameter = Double(calculateParameter(ends.start, ends.end, drag.location))
256-
self.impactHandler(parameter == 1 || parameter == 0)
257-
self.value = (self.range.upperBound-self.range.lowerBound)*parameter + self.range.lowerBound
258-
self.isActive = true
259-
})
260-
.onEnded({ (drag) in
261-
let ends = self.calculateEndPoints(proxy)
262-
let parameter = Double(calculateParameter(ends.start, ends.end, drag.location))
263-
self.impactHandler(parameter == 1 || parameter == 0)
264-
self.value = (self.range.upperBound-self.range.lowerBound)*parameter + self.range.lowerBound
265-
self.isActive = false
266-
})
267-
).allowsHitTesting(!self.isDisabled)
268-
}
269-
}
270-
)
271-
}.frame(idealWidth: 200, idealHeight: 50)
272-
.coordinateSpace(name: space)
278+
GeometryReader { proxy in
279+
self.style.makeThumb(configuration: self.configuration)
280+
.position(x: proxy.size.width/2, y: proxy.size.height/2)
281+
.offset(self.thumbOffset(proxy))
282+
.gesture(self.makeGesture(proxy)).allowsHitTesting(!self.isDisabled)
283+
}
284+
}
285+
.coordinateSpace(name: space)
273286
}
274287
}
275288

289+

Sources/Sliders/TrackPad.swift

+21-24
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public struct TrackPad: View {
175175
self.rangeY = rangeY
176176
self.isDisabled = isDisabled
177177
}
178-
178+
179179
public init(_ value: Binding<CGPoint>){
180180
self._value = value
181181
}
@@ -185,8 +185,8 @@ public struct TrackPad: View {
185185
self.rangeX = range
186186
self.rangeY = range
187187
}
188-
189-
188+
189+
190190
private var configuration: TrackPadConfiguration {
191191
.init(isDisabled: isDisabled,
192192
isActive: isActive,
@@ -199,7 +199,7 @@ public struct TrackPad: View {
199199
minY: Double(rangeY.lowerBound),
200200
maxY: Double(rangeY.upperBound))
201201
}
202-
202+
203203
// MARK: Calculations
204204
// Limits the value of the drag gesture to be within the frame of the trackpad
205205
// If the gesture hits an edge of the trackpad a haptic impact is played, an state
@@ -220,7 +220,7 @@ public struct TrackPad: View {
220220
} else {
221221
self.atXLimit = false
222222
}
223-
// vertical haptix handling
223+
// vertical haptic handling
224224
if pctY == 1 || pctY == 0 {
225225
if !self.atYLimit {
226226
self.impactOccured()
@@ -241,7 +241,7 @@ public struct TrackPad: View {
241241
let pctY = (value.y - rangeY.lowerBound)/(rangeY.upperBound - rangeY.lowerBound)
242242
return CGSize(width: w*(pctX-0.5), height: h*(pctY-0.5))
243243
}
244-
244+
245245
// MARK: Haptics
246246
private func impactOccured() {
247247
#if os(macOS)
@@ -254,24 +254,21 @@ public struct TrackPad: View {
254254
public var body: some View {
255255
ZStack {
256256
style.makeTrack(configuration: configuration)
257-
.overlay(GeometryReader { proxy in
258-
ZStack {
259-
self.style.makeThumb(configuration: self.configuration)
260-
.offset(self.thumbOffset(proxy))
261-
.gesture(
262-
DragGesture(minimumDistance: 0, coordinateSpace: .named(self.space))
263-
.onChanged({
264-
self.constrainValue(proxy, $0.location)
265-
self.isActive = true
266-
})
267-
.onEnded({
268-
self.constrainValue(proxy, $0.location)
269-
self.isActive = false
270-
}))
271-
}
272-
})
257+
GeometryReader { proxy in
258+
self.style.makeThumb(configuration: self.configuration)
259+
.position(x: proxy.size.width/2, y: proxy.size.height/2)
260+
.offset(self.thumbOffset(proxy))
261+
.gesture(
262+
DragGesture(minimumDistance: 0, coordinateSpace: .named(self.space))
263+
.onChanged({
264+
self.constrainValue(proxy, $0.location)
265+
self.isActive = true
266+
})
267+
.onEnded({
268+
self.constrainValue(proxy, $0.location)
269+
self.isActive = false
270+
}))
271+
}
273272
}.coordinateSpace(name: space)
274-
275273
}
276274
}
277-

0 commit comments

Comments
 (0)