I’ve a Customized Horizontal Scroll Wheel that connects a mannequin that saves worth in a UserDefault utilizing didset. After I use a daily HStack the worth is appropriate and saves accurately to UserDefaults while you scroll to a brand new quantity however the issue is when the view is initially proven its at all times at a 0 when after I affirm the worth handed in isnt zero. After I change it to a LazyHStack it really works as indented however comes with bizarre bugs the place it doesn’t scroll to the proper place and typically breaks the scrolling. :
The place the userDefaults is being set.
@Printed var captureInterval: Int = 1 {
didSet {
UserDefaults.commonplace.set(captureInterval, forKey: "captureInterval")
}
}
@Printed var startCaptureInterval: Int = 2 {
didSet {
UserDefaults.commonplace.set(startCaptureInterval, forKey: "startCaptureInterval")
}
}
The View in Query:
struct WheelPicker: View {
var rely: Int //(20 handed in)
var spacing: CGFloat = 80
@Binding var worth: Int
//TODO: Add some Haptic Suggestions
var physique: some View {
GeometryReader { geometry in
let measurement = geometry.measurement //Measurement of all the Mum or dad Container
let horizontalPadding = geometry.measurement.width / 2
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: spacing) {
ForEach(0..(get: { //wanted as a result of scrollPositon wont settle for a Binding int
let place: Int? = worth
return place
}, set: { newValue in
if let newValue {
worth = newValue //merely taking within the new worth and move it again to the binding
}
}))
.overlay(alignment: .middle, content material: {
Rectangle() //will peak the energetic index in wheelpicker by drawing a small rectangle over it
.body(width: 1, peak: 45) //you'll be able to regulate its peak to make it larger
})
.safeAreaPadding(.horizontal, horizontalPadding) //makes it begin and finish within the middle
}
}
}
#Preview {
@Previewable @State var rely: Int = 30
@Previewable @State var worth: Int = 5
WheelPicker(rely: rely, worth: $worth)
}
What I’ve tried to date its to create an Int? variable that I initialize in .job to equals he worth, move it in as a the scrollPosition and add an OnTap Within the HStack to set each the worth and new Int? variable. This makes the scrolling and preliminary place work good however wont set the userDefaults anymore.