Как определить жест перетаскивания в свойстве в представлении SwiftUI

Я не могу вернуть 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
                        }
                    }
            })
    }

2 ответа

Как оно возвращается_EndedGesture<_ChangedGesture<DragGesture>>и неDragGesture, можно объявить вычисляемое свойство следующим образом:

       var dimDrag: some Gesture {
        DragGesture()
            .onChanged({
                    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
                        }
                    }
            })
    }

Эту переменную можно сделать частью частного расширения.

Для меня работает следующее:

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

        })
        .onEnded { value in

        }
    }()
Другие вопросы по тегам