[英]SwiftUI DragGesture is freeze
我试图用手势做一个可调整大小的视图。
问题是,当我向左或向右移动手势 object 时,应用程序将冻结,处理器使用率为 100%。 它是两个Text
视图之间的循环。
我做错了什么,我该如何纠正?
struct TestView2 : View {
@State private var width : CGFloat = 400.0
var body : some View {
VStack {
ZStack(alignment: .bottomTrailing) {
Text("\(width)")
.frame(width: width)
Text(":")
.frame(width: 10, height: 30)
.background(.bar)
.gesture(
DragGesture()
.onChanged { value in
width = max(100, width + value.translation.width)
print(width)
}
)
}
.frame(width: width, height: 30, alignment: .topLeading)
.border(.gray, width: 1)
.background(.green)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
看法:
调试器:
对我来说,解决方案是将手势的coordinateSpace
显式设置为.global
:
DragGesture(coordinateSpace: .global)
.onChanged { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.