【问题标题】:onTapGesture and onLongPressGesture not responsiveonTapGesture 和 onLongPressGesture 没有响应
【发布时间】:2020-03-07 09:50:53
【问题描述】:

我有一个 SwiftUI 按钮,最初我在操作区域内处理按钮按下,但现在我想更改按钮,以便我也可以长按它。为了做到这一点,我现在必须创建一个 onTapGesture 和 onLongPressGesture。在我的 Watch 上测试我的应用程序后,我意识到使用 onTapGesture 和 onLongpressGesture 按钮的响应速度较慢。我可以明显看到按钮被按下,但它有时只处理动作 1 和动作 2。有没有人有类似的经历,解决方法是什么?

 Button(action: {
                  // action 1
                }) {
                  Image(systemName: "checkmark")

                    .onTapGesture (count:1){
                      // action 1
                  }

              .onLongPressGesture {
                // action 2
              }
            }

【问题讨论】:

  • 我也遇到了同样的问题,哈哈...检查了下面的答案后,我仍然不知道如何解决它。 : - /

标签: swiftui


【解决方案1】:

不要使用按钮。

Image(systemName: "checkmark")
.onTapGesture {
    // action 1
}
.onLongPressGesture {
    // action 2
}

或者向按钮添加同时手势

Button(action: {
    print("tap")
}) {
    Image(systemName: "trash")
}.simultaneousGesture(LongPressGesture(minimumDuration: 1, maximumDistance: 10).onEnded({ (b) in
    print("long")
}))

警告,“额外”点击将跟随每个长事件...

你可以玩手势蒙版,比如说

Button(action: {
    print("tap")
}) {
    Image(systemName: "trash")
}.simultaneousGesture(LongPressGesture().onChanged({ (b) in
    print("long change", b)
})
    .onEnded({ (b) in
    print("long end")
}), including: .gesture)

打印

long change true // on tap

// on long press
long change true
long end

但没有“点击”!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2012-02-08
    • 2019-07-20
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多