I tried to encapsulate the gesture in a class and then call it in another view. My project can run well, and it can be built well. but Xcode12 give me a pink error. This is my code

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)
        }
    }
}
1

There are 1 best solutions below

0
On

I deleted this code in the computed property, and the error disappeared. But i really don't know why。

.updating($magnificationLevel, body:{(value,state,_) in
           return state=value
        })