简体   繁体   English

SwiftUI:当用户从 longPressGesture 释放屏幕时如何运行操作?

[英]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.

相关问题 SwiftUI LongPressGesture 需要很长时间才能识别 TapGesture 何时也存在 - SwiftUI LongPressGesture takes too long to recognize when TapGesture also present 如何从 swiftui 导航堆栈中删除后退按钮,但允许用户在从屏幕左侧捏住屏幕时向后移动 - How to remove back button from swiftui navigation stack but allow the user to move back when the screen is pinched from the left side of the screen iOS-用户滑动屏幕时如何执行操作? - iOS - How do I perform an action when the user swipes the screen? 当用户将手指从屏幕上松开时,使用 Xcode 中的委托函数生成图像 - Using the delegate function in Xcode to produce an image when the user releases their finger off of the screen 如何使带有 TapGesture 和 LongPressGesture 的 NavigationLink 在 SwiftUI 中同时工作? - How can I make a NavigationLink with TapGesture and LongPressGesture working simultaneously together in SwiftUI? 使用 SwiftUI 将用户从登录屏幕带到另一个屏幕? - Using SwiftUI to bring the user from the Sign In screen to another screen? SwiftUI TapGesture 和 LongPressGesture 在 ScrollView 中,点击指示不起作用 - SwiftUI TapGesture and LongPressGesture in ScrollView with tap indication not working SwiftUI 检测用户何时截屏或录屏 - SwiftUI detect when the user takes a screenshot or screen recording 如何从 SwiftUI 的列表中获取屏幕行? - How to get on screen rows from List in SwiftUI? 使用 LongPressGesture 多次触发一个动作 - Use LongPressGesture to fire an action multiple time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM