如何在SwiftUI视图上的属性中定义拖动手势

时间:2019-12-06 19:06:22

标签: swiftui draggesture

我无法从如下所示的计算属性中返回DragGesture

'_EndedGesture<_ChangedGesture<DragGesture>>' to return type 'DragGesture'


 var dimDrag : DragGesture {
        DragGesture()
            .onChanged({
                    print("Dim drag")

                    if $0.translation.width > 0 {
                        self.model.menuOffset = max(min($0.translation.width, UIScreen.main.bounds.width*0.7), 0.0)
                    } else {
                        self.model.menuOffset = max(min(UIScreen.main.bounds.width*0.7 + $0.translation.width, UIScreen.main.bounds.width*0.7), 0.0)
                    }

            })
            .onEnded({
                    if $0.translation.width < -100 {
                        withAnimation {
                            self.model.isMenuOpen = true
                            self.model.menuOffset = 0.0
                        }
                    } else if $0.translation.width > 100 {
                        withAnimation {
                            self.model.isMenuOpen = false
                            self.model.menuOffset = UIScreen.main.bounds.width*0.7
                        }
                    }
            })
    }

1 个答案:

答案 0 :(得分:0)

以下对我有用:

let myDrag = {
    DragGesture()
        .onChanged({ value in

        })
        .onEnded { value in

        }
    }()