【问题标题】:How to detect EdgePan Gesture SwiftUI如何检测 EdgePan 手势 SwiftUI
【发布时间】:2019-12-21 04:14:23
【问题描述】:

我试图在 SwiftUI 中从左侧识别 EdgePan。我知道有一些手势,但无法将它们应用于整个屏幕。 (尝试使用 DragGesture)。 是否可以在 SwiftUI 中实现 EdgePan? 或者我如何使用 DragGesture 来做同样的事情?

【问题讨论】:

    标签: swift gesture swiftui


    【解决方案1】:

    是的,DragGesture 可以做到这一点。

    DragGesture 有一个startLocation 属性,即CGPoint。从中,您可以检测手势从哪里开始,并使用它来确定它是否从边缘开始。

    取你现有的DragGesture,在.onChanged闭包中,传入gesture,然后用gesture.startLocation找到起始位置。由于您想检测边缘,因此您需要gesture.startLocationx 属性。

    看起来像这样:

    DragGesture()
        .onChanged({gesture in
            if gesture.startLocation.x < CGFloat(100.0){
                print("edge pan")
            }
         }
    )
    

    【讨论】:

    • 哦,我忘记了帖子:D 是的,我做了同样的事情,但使用了.onEnded
    • 效果很好的唯一问题是它没有考虑方向本身。假设您在左边缘并向上滑动,这会将其视为边缘平移,但我们想要水平移动。我将打印包裹在这段代码中,以确保角度为 +45* 或 -45*,这对我来说足够接近以保持水平...让坡度 = abs((gesture.startLocation.y -gesture.location.y) /(gesture.startLocation.x -gesture.location.x)) if slope
    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多