gpt4 book ai didi

ios - 如何从SwiftUI中的struct返回值?

转载 作者:行者123 更新时间:2023-12-01 16:12:20 25 4
gpt4 key购买 nike

我根据以下视频制作了RangeSlider:https://www.youtube.com/watch?v=5whzc_9eR78。它工作正常,但是我想从中获取左右值,我该怎么做?

我的代码:

struct RangeSlider: View {

@State var width: CGFloat = 0
@State var width1: CGFloat = 20
@State var totalWidth: CGFloat = UIScreen.main.bounds.width-60

let circleSize: CGFloat = 21

var body: some View {
VStack{
ZStack(alignment: .leading){
Rectangle()
.fill(Color.black.opacity(0.2))
.frame(width: self.totalWidth+2*self.circleSize, height: self.circleSize/3)

Rectangle()
.fill(Color.black)
.frame(width: self.width1-self.width, height: self.circleSize/3)
.offset(x: self.width+self.circleSize)

HStack(spacing: 0){
Circle()
.fill(Color.white)
.frame(width: self.circleSize, height: self.circleSize)
.offset(x: self.width)
.gesture(
DragGesture()
.onChanged({ (value) in
if (value.location.x >= 0 && value.location.x <= self.width1){

self.width = value.location.x

}
}))

Circle()
.fill(Color.white)
.frame(width: self.circleSize, height: self.circleSize)
.offset(x: self.width1)
.gesture(
DragGesture()
.onChanged({ (value) in
if (value.location.x <= self.totalWidth-(2*self.circleSize) && value.location.x >= self.width){

self.width1 = value.location.x

}
}))
}

}
}
}
}

我正在ContentView.swift的ZStack中调用 RangeSlider(),我想返回左拇指值 width/totalWidth和右拇指值 width1/totalWidth,就像在Textfield(text:self。$ value)中完成的操作一样,将文本返回到变量“值”。

最佳答案


@Binding var left: CGFloat
@Binding var right: CGFloat

然后在 widthwidth1更改时进行设置。构造RangeSlider时,您需要传递Bindings: RangeSlider(left: $left, right: $right)

关于ios - 如何从SwiftUI中的struct返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61762348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com