如何在SwiftUI中将DragGesture转换为单独的函数?

如何在SwiftUI中将DragGesture转换为单独的函数?,swiftui,Swiftui,我有两个简单的视图,第一个是更新的,第二个是更新的,我想让我的身体更干净,我想传递这些功能,我们怎么能做到?我认为这是一种clouser函数 struct ContentView: View { @State private var location: CGSize = CGSize() @GestureState private var translation: CGSize = CGSize() va

我有两个简单的视图,第一个是更新的,第二个是更新的,我想让我的身体更干净,我想传递这些功能,我们怎么能做到?我认为这是一种clouser函数

        struct ContentView: View {
        
        @State private var location: CGSize = CGSize()
        @GestureState private var translation: CGSize = CGSize()
    
        var body: some View {
            Circle()
                .fill()
                .frame(width: 100, height: 100, alignment: .center)
                .position(x: location.width + translation.width + 100, y: location.height + translation.height + 100)
                .gesture(
                    DragGesture()
                        
                        // transfer to updatingFunction
                        .updating($translation) { value, state, _ in
                            state = value.translation
                        }
                        
                        
                        // transfer to onEndedFunction
                       .onEnded { value in onEndedFunction(value: value)}
                    
                )
            
            
            
        }
        
        func updatingFunction() {
            
        }
        
    func onEndedFunction(value: DragGesture.Value) {
        
        location = CGSize(width: location.width + value.translation.width, height: location.height + value.translation.height)
        
    }
        
        
    }

它看起来不太合理,相反,我建议将整个手势分开(如果您希望代码简化、可读性等)

以下是可能的变体:

struct ContentView: View {
    
    @State private var location: CGSize = CGSize()
    @GestureState private var translation: CGSize = CGSize()
    
    private var dragGesture: some Gesture {
        DragGesture()
            .updating($translation) { value, state, _ in
                state = value.translation
            }
            .onEnded { value in
                location = CGSize(width: location.width + value.translation.width, height: location.height + value.translation.height)
            }
    }
    
    var body: some View {
        Circle()
            .fill()
            .frame(width: 100, height: 100, alignment: .center)
            .position(x: location.width + translation.width + 100, y: location.height + translation.height + 100)
            .gesture(dragGesture)
    }
}

它看起来不太合理,相反,我建议将整个手势分开(如果您希望代码简化、可读性等)

以下是可能的变体:

struct ContentView: View {
    
    @State private var location: CGSize = CGSize()
    @GestureState private var translation: CGSize = CGSize()
    
    private var dragGesture: some Gesture {
        DragGesture()
            .updating($translation) { value, state, _ in
                state = value.translation
            }
            .onEnded { value in
                location = CGSize(width: location.width + value.translation.width, height: location.height + value.translation.height)
            }
    }
    
    var body: some View {
        Circle()
            .fill()
            .frame(width: 100, height: 100, alignment: .center)
            .position(x: location.width + translation.width + 100, y: location.height + translation.height + 100)
            .gesture(dragGesture)
    }
}

您可以使用
inout
来传递状态

struct ContentViewGesture: View {
    @State private var location: CGSize = CGSize()
    @GestureState private var translation: CGSize = CGSize()
    
    var body: some View {
        Circle()
            .fill()
            .frame(width: 100, height: 100, alignment: .center)
            .position(x: location.width + translation.width + 100, y: location.height + translation.height + 100)
            .gesture(
                DragGesture()
                    // transfer to updatingFunction
                    .updating($translation) { value, state, _ in
                        updatingFunction(value: value, state: &state) //<<-- Here
                    }
                    
                    // transfer to onEndedFunction
                    .onEnded { value in onEndedFunction(value: value)}
                
            )
    }
    
    func updatingFunction(value: DragGesture.Value, state: inout CGSize) { //<<-- Here
        state = value.translation
    }
    
    func onEndedFunction(value: DragGesture.Value) {
        location = CGSize(width: location.width + value.translation.width, height: location.height + value.translation.height)
    }
}
struct contentview手势:查看{
@国家私有变量位置:CGSize=CGSize()
@GestureState私有变量转换:CGSize=CGSize()
var body:一些观点{
圈()
.fill()
.框架(宽度:100,高度:100,对齐:。中心)
.位置(x:location.width+translation.width+100,y:location.height+translation.height+100)
.手势(
德拉格斯特尔()
//转移到更新函数
.更新($translation){值,状态,uu

更新函数(值:值,状态:&状态)//您可以使用
inout
传递状态

struct ContentViewGesture: View {
    @State private var location: CGSize = CGSize()
    @GestureState private var translation: CGSize = CGSize()
    
    var body: some View {
        Circle()
            .fill()
            .frame(width: 100, height: 100, alignment: .center)
            .position(x: location.width + translation.width + 100, y: location.height + translation.height + 100)
            .gesture(
                DragGesture()
                    // transfer to updatingFunction
                    .updating($translation) { value, state, _ in
                        updatingFunction(value: value, state: &state) //<<-- Here
                    }
                    
                    // transfer to onEndedFunction
                    .onEnded { value in onEndedFunction(value: value)}
                
            )
    }
    
    func updatingFunction(value: DragGesture.Value, state: inout CGSize) { //<<-- Here
        state = value.translation
    }
    
    func onEndedFunction(value: DragGesture.Value) {
        location = CGSize(width: location.width + value.translation.width, height: location.height + value.translation.height)
    }
}
struct contentview手势:查看{
@国家私有变量位置:CGSize=CGSize()
@GestureState私有变量转换:CGSize=CGSize()
var body:一些观点{
圈()
.fill()
.框架(宽度:100,高度:100,对齐:。中心)
.位置(x:location.width+translation.width+100,y:location.height+translation.height+100)
.手势(
德拉格斯特尔()
//转移到更新函数
.更新($translation){值,状态,uu
更新函数(值:值、状态:&状态)//