iPhone 中何时调用触摸取消方法?
我能够理解,当用户只是触摸视图时,触摸开始和结束被调用。当用户在视图上滑动手时,触摸 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 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).
注意:如果您在
touchesBegan
之后启动UIView
动画,触摸也会被取消。为了防止这种情况,请确保包含UIViewAnimationOptionAllowUserInteraction:
例如
Note: touches also get cancelled if you start a
UIView
animation aftertouchesBegan
. To prevent this make sure you includeUIViewAnimationOptionAllowUserInteraction:
e.g.
来自 Apple 参考文档
From the Apple Reference documents
并且,来自 iOS 事件处理指南,第 17 页19:
And, from the Event Handling Guide for iOS, p. 19:
我正在
UIScrollView
下的视图上处理touchesBegan()
/touchesMoved()
,这很有挑战性。当我捏的时候,我的触摸被某个地方取消了(不知何故,单次触摸移动就可以了),我正在研究如何停止被取消。我发现,UIScrollView 上有一个属性Cancel On Scroll
,如果您的情况与我的情况类似,您可以将其选中以停止被取消。听起来您的触摸被取消的情况有很多,所以我的回答只是其中之一。
I was handling
touchesBegan()
/touchesMoved()
on a view underUIScrollView
, 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 propertyCan 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.