1

我有一个 UIView 子类,我在其中使用 touchesBegan、touchesMoved、touchesEnded 处理触摸。我注意到当我在内部开始触摸然后在 UIView touchesEnded 外部拖动时不会触发,当我在 UIView 框架外拖动时有没有办法调用它?谢谢

4

3 回答 3

1

请改用 touchesCancelled。

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}  
于 2012-10-23T10:03:41.123 回答
1

可能它正在调用- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

请检查UIResponder 类参考

touchesCancelled:withEvent:

当系统事件(例如内存不足警告)取消触摸事件时发送到接收器。

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

参数

触动

A set of UITouch instances that represent the touches for the ending phase of the event represented by event. event

An object representing the event to which the touches belong.

讨论

该方法在Cocoa Touch框架接收到需要取消触摸事件的系统中断时调用;为此,它会生成一个具有 UITouchPhaseCancel 阶段的 UITouch 对象。中断可能会导致应用程序不再处于活动状态或视图从窗口中删除

当一个对象接收到 touchesCancelled:withEvent: 消息时,它应该清除在其 touchesBegan:withEvent: 实现中建立的任何状态信息。

此方法的默认实现什么也不做。然而,UIResponder 的直接 UIKit 子类,尤其是 UIView,将消息转发到响应者链。要将消息转发给下一个响应者,请将消息发送给 super(超类实现);不要将消息直接发送给下一个响应者。例如,

[超级 touchesCancelled:touches withEvent:event];

如果您在不调用 super 的情况下覆盖此方法(一种常见的使用模式),那么您还必须覆盖用于处理触摸事件的其他方法,如果只是作为存根(空)实现。可用性

Available in iOS 2.0 and later.
于 2012-10-23T10:06:07.003 回答
0

即使你在你的视野之外,你仍然应该进入touchesMovedviewController的视野。我会将数组添加到您的自定义UIView中,这将保留此视图分配给的触摸对象并实现以下逻辑:

  1. 当您收到时touchesBegan- 为给定的触摸坐标匹配位置将触摸对象添加到数组UIView中(您可能必须遍历所有子视图并将坐标与框架匹配以找到正确的子视图)
  2. 当您收到时touchesMoved- 从UIView's阵列中删除先前位置的触摸并将其添加到UIView当前位置(如果有)
  3. 当您收到touchesEnded& - 从所有阵列touchesCancelled中删除触摸UIViews

当您从自定义 UIView 数组中删除或添加触摸对象时,您可能会调用委托方法(实现观察者模式),因为此时您知道它是真正按下还是未按下事件。请记住,您可能在一个数组中有许多触摸对象,因此在调用您的委托之前,请检查您是添加了第一个对象还是删除了最后一个对象,因为这是您应该调用您的委托的唯一情况。

我在上一场比赛中实现了这样的解决方案,但UIViews我没有虚拟按钮。

于 2012-10-23T10:30:12.123 回答