iOS 检测用户触摸释放

发布于 2024-12-03 19:03:01 字数 736 浏览 0 评论 0 原文

这可能已发布在某处,但我找不到。我正在编写一个带有两个 UIView 的简单 iOS 应用程序。用户将首先按住屏幕的某个区域,然后释放其触摸,然后快速触摸下面的第二个视图。

第一个 UIView 附加了一个 UILongPressGestureRecognizer 并且工作正常。第二个 UIView 附加了一个 UITapGestureRecognizer 并且也可以正常工作。但是,我无法让这些手势识别器返回任何表明用户释放了触摸的内容。

我尝试过这段代码但没有成功:

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (UIGestureRecognizerStateRecognized) {
        holdLabel.text = @"Holding Correctly. Release Touch when ready.";
        holdView.backgroundColor = [UIColor greenColor];
    } else if (UIGestureRecognizerStateCancelled){
        holdLabel.text = @"Ended";
        holdView.backgroundColor = [UIColor redColor];
}

任何建议都会很棒,特别是如果有人知道如何实现返回用户触摸设备状态的调用。我查看了开发人员文档,结果一无所获。

This may have been posted here somewhere but I can't find it. I am writing a simple iOS app with two UIViews. The user will first press and hold a certain area of the screen then release their touch on it then quickly touching a second view below.

The first UIView has a UILongPressGestureRecognizer attached to it and works fine. The second UIView has a UITapGestureRecognizer attached to it and also works fine. I cannot however, get either of these gesture recognizers to return anything that states that the user released their touch.

I have tried this code to no avail:

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (UIGestureRecognizerStateRecognized) {
        holdLabel.text = @"Holding Correctly. Release Touch when ready.";
        holdView.backgroundColor = [UIColor greenColor];
    } else if (UIGestureRecognizerStateCancelled){
        holdLabel.text = @"Ended";
        holdView.backgroundColor = [UIColor redColor];
}

Any suggestions would be great and especially if someone knows how to implement a call that returns the state of a user touching the device. I've looked over the developer docs and have come up empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杆小烟枪 2024-12-10 19:03:02

经过几个小时的修补后,我找到了一种可行的方法,不确定这是否是最好的方法。事实证明我需要像下面的代码一样编写它。我没有调用我在 viewDidLoad() 方法中声明的特定 UIGestureRecognizer。

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (holdRecognizer.state == UIGestureRecognizerStateBegan) {
        holdLabel.text = @"Holding Correctly. Release when ready.";
        holdView.backgroundColor = [UIColor greenColor];
    } else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
    {
        holdLabel.text = @"You let go!";
        holdView.backgroundColor = [UIColor redColor];
    }
}

After tinkering for a couple hours, I found a way that is working, not sure if its the best way to do it. Turns out I need to be writing it like the code below. I wasn't calling the specific UIGestureRecognizer I was declaring in the viewDidLoad() method.

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (holdRecognizer.state == UIGestureRecognizerStateBegan) {
        holdLabel.text = @"Holding Correctly. Release when ready.";
        holdView.backgroundColor = [UIColor greenColor];
    } else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
    {
        holdLabel.text = @"You let go!";
        holdView.backgroundColor = [UIColor redColor];
    }
}
冷︶言冷语的世界 2024-12-10 19:03:02

您需要在此处使用手动触摸处理(而不是使用手势识别器)。任何 UIResponder 子类都可以实现以下四种方法:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

通过使用这些方法,您可以访问触摸事件的每个阶段。您可能必须实现自己的逻辑来检测长按,但您可以完全访问所有触摸。

有关触摸处理的更多信息,WWDC 2011 的此会议是黄金会议(需要开发帐户):

https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296

You need to use manual touch handling here (as opposed to using a gesture recognizer). Any UIResponder subclass can implement the following four methods:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

By using these methods, you get access to every phase of the touch events. You might have to implement your own logic to detect the long press, but you have full access to all touches.

For more information on touch handling, this session from WWDC 2011 is golden (requires dev account):

https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296

红衣飘飘貌似仙 2024-12-10 19:03:02

斯威夫特4+

 let gesture = UILongPressGestureRecognizer(target: self, action:  #selector(self.checkAction))
    self.view.addGestureRecognizer(gesture)


     @objc func checkAction(sender : UILongPressGestureRecognizer) {
        if sender.state == .ended || sender.state == .cancelled || sender.state == .failed {

        }
    }

Swift 4+

 let gesture = UILongPressGestureRecognizer(target: self, action:  #selector(self.checkAction))
    self.view.addGestureRecognizer(gesture)


     @objc func checkAction(sender : UILongPressGestureRecognizer) {
        if sender.state == .ended || sender.state == .cancelled || sender.state == .failed {

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文