我试图将手势封装在一个类中,然后在另一个视图中调用它。我的项目可以运行良好,也可以构建良好。但是 Xcode12 给了我一个粉红色的错误。这是我的代码
class GestureClass: ObservableObject {
private var minZoom:CGFloat=1
private var maxZoom:CGFloat=4.00
@GestureState private var magnificationLevel:CGFloat=1
@Published public var zoomLevel:CGFloat=1
@Published public var current:CGFloat=1
var magnify: some Gesture {
MagnificationGesture()
.updating($magnificationLevel, body:{(value,state,_) in
return state=value
})
.onChanged({ value in
self.current = value
})
.onEnded({ value in
self.zoomLevel = self.setZoom(value)
self.current = 1
})
}
func setZoom(_ gesturevalue:CGFloat) -> CGFloat {
return max(min(self.zoomLevel*gesturevalue, self.maxZoom), self.minZoom)
}
func gestureresult() -> CGFloat {
return self.setZoom(self.current)
}
}
struct GestureModelView: View {
@EnvironmentObject var gesturemodel:GestureClass
var body: some View {
let myges = gesturemodel.magnify
HStack {
Rectangle()
.foregroundColor(.blue)
.frame(width:200*gesturemodel.gestureresult(), height:200)
.gesture(myges)
}
}
}