作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我根据以下视频制作了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
}
}))
}
}
}
}
}
RangeSlider()
,我想返回左拇指值
width/totalWidth
和右拇指值
width1/totalWidth
,就像在Textfield(text:self。$ value)中完成的操作一样,将文本返回到变量“值”。
最佳答案
加
@Binding var left: CGFloat
@Binding var right: CGFloat
width
和
width1
更改时进行设置。构造RangeSlider时,您需要传递Bindings:
RangeSlider(left: $left, right: $right)
关于ios - 如何从SwiftUI中的struct返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61762348/
我是一名优秀的程序员,十分优秀!