iPhone 中何时调用触摸取消方法?

发布于 2024-11-02 08:23:24 字数 103 浏览 4 评论 0原文

我能够理解,当用户只是触摸视图时,触摸开始和结束被调用。当用户在视图上滑动手时,触摸 Moved 方法将被调用。但是,什么时候调用 TouchCanceled 或通过用户的什么操作调用此方法?

I am able to understand that when user just touches the view, touches Began and Ended called. When user swipes their hand on a view, touches Moved method gets called. But when does touches Cancelled get called or by what action on user this method gets called?

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

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

发布评论

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

评论(5

不如归去 2024-11-09 08:23:24

我认为 TouchCancelled 被调用的最常见原因(无论如何从 iOS 3.2 开始)可能是 UIGestureRecognizer 对手势的识别之后。如果您的视图附加了任何类型的手势识别器,那么提供 TouchCancelled 方法的自定义实现通常非常重要 - 请注意,这包括使用手势识别器的现成视图,包括 UIScrollView。

默认情况下,手势识别器在识别后会取消向命中测试视图传送触摸,但可以禁用此行为。这涉及到将touchesCancelled消息发送到该视图,很可能是在touchesBegan或touchesMoved消息之后。如果您的触摸处理代码依赖于在 TouchsEnded 方法中实现的代码,则该方法可能永远不会被触发,并且可能会发生某种严重问题,因此需要正确地解决 TouchsCancelled 中的任何松散问题。

手势识别器功能的来龙去脉显然比我在这里提到的要复杂一些 - 我强烈推荐阅读Apple的手势识别器文档。

另外,请查看有关手势识别器的 WWDC 视频(从 2010 年开始)。

I think probably the most common reason for touchesCancelled being called (since iOS 3.2 anyway) is following the recognition of a gesture by a UIGestureRecognizer. If your view has any kind of gesture recognizer attached to it then it is often very important to provide a custom implementation of the touchesCancelled method - note this includes ready made views that use gesture recognizers, including UIScrollView.

By default, gesture recognizers cancel the delivery of touches to the hit-test view upon recognition, although this behaviour can be disabled. This involves sending the touchesCancelled message to that view, most likely following a touchesBegan or touchesMoved message. If your touch handling code is relying on code implemented in the touchesEnded method, it is possible this may never be fired and some kind of serious problem could occur, hence the need to properly tie up any loose ends in touchesCancelled.

The ins and outs of gesture recognizer functionality is obviously a bit more complex than I've mentioned here - I would thoroughly recommend reading Apple's Gesture Recognizers documentation.

Also, check out the WWDC videos on gesture recognizers (starting from 2010).

忱杏 2024-11-09 08:23:24

注意:如果您在 touchesBegan 之后启动 UIView 动画,触摸也会被取消。为了防止这种情况,请确保包含 UIViewAnimationOptionAllowUserInteraction:

例如

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.aView.hidden = NO;
            self.aView.alpha = 1;
        } completion:nil];

Note: touches also get cancelled if you start a UIView animation after touchesBegan. To prevent this make sure you include UIViewAnimationOptionAllowUserInteraction:

e.g.

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.aView.hidden = NO;
            self.aView.alpha = 1;
        } completion:nil];
锦上情书 2024-11-09 08:23:24

来自 Apple 参考文档

系统发送给接收者
事件(例如内存不足警告)
取消触摸事件。

讨论

当Cocoa
Touch架构接收系统
需要取消的中断
触摸事件;为此,它
生成一个 UITouch 对象
UITouchPhaseCancel 的阶段。这
中断是可能的事情
导致应用程序不再
活动视图或要从中删除的视图
窗户

当一个对象接收到一个
TouchsCancelled:withEvent: 向其发送消息
应该清理任何状态信息
是在其设立的
触摸开始:withEvent:
实施。

这个的默认实现
方法什么也不做。然而立即
UIKit 的 UIResponder 子类,
特别是 UIView,转发
向响应者链发送消息。

From the Apple Reference documents

Sent to the receiver when a system
event (such as a low-memory warning)
cancels a touch event.

Discussion

This method is invoked when the Cocoa
Touch framework receives a system
interruption requiring cancellation of
the touch event; for this, it
generates a UITouch object with a
phase of UITouchPhaseCancel. The
interruption is something that might
cause the application to be no longer
active or the view to be removed from
the window

When an object receives a
touchesCancelled:withEvent: message it
should clean up any state information
that was established in its
touchesBegan:withEvent:
implementation.

The default implementation of this
method does nothing. However immediate
UIKit subclasses of UIResponder,
particularly UIView, forward the
message up the responder chain.

马蹄踏│碎落叶 2024-11-09 08:23:24

并且,来自 iOS 事件处理指南,第 17 页19:

当系统事件(例如来电)取消触摸序列时,它会发送touchesCancelled:withEvent:消息。

And, from the Event Handling Guide for iOS, p. 19:

It sends the touchesCancelled:withEvent: message when the touch sequence is cancelled by a system event, such as an incoming phone call.

蓝颜夕 2024-11-09 08:23:24

输入图片这里的描述

我正在 UIScrollView 下的视图上处理 touchesBegan()/touchesMoved(),这很有挑战性。当我捏的时候,我的触摸被某个地方取消了(不知何故,单次触摸移动就可以了),我正在研究如何停止被取消。我发现,UIScrollView 上有一个属性 Cancel On Scroll ,如果您的情况与我的情况类似,您可以将其选中以停止被取消。

听起来您的触摸被取消的情况有很多,所以我的回答只是其中之一。

enter image description here

I was handling touchesBegan()/touchesMoved() on a view under UIScrollView, which is challenging. My touches kept cancelled by somewhere when I pinch (somehow it is OK with single touch movement), I was investigating how to stop being cancelled. I figured out, that there is a property Can Cancel On Scroll on UIScrollView, and you may check it off to stop being cancelled, if your case is similar to my case.

It sounds there are many cases where your touches are being cancelled, so my answer is just one of them.

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