[英]SwiftUI: How to run action when user releases screen from a longPressGesture?
I have a rectangle setup with a long press gesture (0.5 second duration), I want a code snippet to run when the long press gesture starts, when it hits the 0.5 second minimum duration, and when the user releases the screen.我有一个长按手势(持续时间 0.5 秒)的矩形设置,我希望在长按手势开始、达到 0.5 秒最小持续时间以及用户释放屏幕时运行代码片段。 I am having trouble on how to run the code when the user releases the screen.当用户释放屏幕时,我无法运行代码。 My code right now sits in a UIView struct, but I would be willing to move it into a separate class.我的代码现在位于 UIView 结构中,但我愿意将其移动到单独的 class 中。
Currently my code turns the rectangle to green when the gesture "ends" (hits 0.5 seconds).目前,当手势“结束”(达到 0.5 秒)时,我的代码将矩形变为绿色。 I want it to do that and then for example change it to blue when I release the screen.我希望它这样做,然后例如在我释放屏幕时将其更改为蓝色。 I have also tried to follow Apple's tutorial on adding interactivity with gestures.我还尝试按照 Apple 关于添加手势交互性的教程进行操作。 Thanks!谢谢!
Rectangle()
.foregroundColor(self.rectColor)
.frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height, alignment: .topTrailing)
.offset(y:60)
.onTapGesture {
if(self.timingImplement.isRunning){
self.timingImplement.stop()
}
}
.gesture(LongPressGesture(minimumDuration: 0.5)
.onEnded{_ in
if(self.timingImplement.isRunning == false){
self.timingImplement.reset()
self.timingImplement.start()
self.timingImplement.genNewScramble()
self.rectColor = Color.green
}
else{
self.timingImplement.stop()
}
})
checkout this Apple example, this does this (and a few thing more you can learn;)查看这个 Apple 示例,这样做(还有一些你可以学习的东西;)
https://developer.apple.com/documentation/swiftui/gestures/composing_swiftui_gestures https://developer.apple.com/documentation/swiftui/gestures/composing_swiftui_gestures
check out this one as well: https://serialcoder.dev/text-tutorials/swiftui/handle-press-and-release-events-in-swiftui/看看这个: https://serialcoder.dev/text-tutorials/swiftui/handle-press-and-release-events-in-swiftui/
this one talks about a modified drag gesture这个谈论修改后的拖动手势
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.