UIView 继承的 UIResponder (负责 UI 事件处理) 类中提供了四个方法处理多点触控:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
touchesBegan 方法在触控开始时被调用,
touchesMoved 方法在触控移动时被调用
touchesEnded 方法在触控结束时被调用
touchesCancelled 方法在系统强制结束触控时被调用
上述方法的参数:
· event 为触控动作引发的 UI 事件,只有发生了 UI 事件 UIResponder 的相应处理方法才会被调用。传递给上述方法的 UI 事件都是 “touch” 类型的,它关联了一系列 UITouch 对象来代表发生该事件时所有处于各种状态 (began, moved, ended, cancelled) 下的触控动作。
· touches 同样是一系列 UITouch 对象的** (NSSet),不过它只包含处于特定状态的触控动作。比方说调用 touchesBegan 时传递的就是所有处在 began 状态 (phase) 的 UITouch 对象。
相关类型说明:
· UIEvent 对象代表了 iPhone OS 中一个UI事件,主要有三种类型:touch,motion,remote control。
· UITouch对象代表一根手指在某一个UI事件发生时刻的动作,它有四种状态(phase),began, moved, ended, cancelled。
· 触控动作会引发 touch 类的UI事件,而这个 UI 事件中也就包括了一系列 UITouch 对象来记录当时的所有手指的动作。当 touch 类型的 UI 事件发生的时候会被交给 UIResponder 处理,继而调用上述四个方法。
OpenGL ES 程序模板中的多点触控处理
· OES 程序模板中的类继承关系为 EAGLView :UIView :UIResponder
· 在 EAGLView 类中实现了上述四个方法,并分别在其中用不同的标签调用 ESRenderer 的 view 方法。
· ESRenderer 协议(就是接口)中提供了四个同名同参的 view 方法,用标签来区分调用:
@optional
- (void)view:(UIView*)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)view:(UIView*)view touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)view:(UIView*)view touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)view:(UIView*)view touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;
· 实现了 ESRenderer 接口的 ES1Renderer 和 ES2Renderer 最终在 View 方法中负责触控事件的处理。
获取各种触控信息的方法
获取触控对象(UITouch)
· 通过 event 参数获取 UITouch **
//所有关联的UITouch
- (NSSet *)allTouches;
//指定窗口的UITouch
- (NSSet *)touchesForWindow:(UIWindow *)window;
//指定View上的UITouch
- (NSSet *)touchesForView:(UIView *)view;
- (NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2);
· touches 参数本身就是处在当前状态的UITouch**
· 从 touches **中随机获取一个UITouch
[touches anyObject];
UITouch 对象的各种属性
· timestamp 这个触控对象最后改变的时间戳。 用从系统启动到现在的时间来表示,float类型。开始时和移动时会更新。
· tapCount 连续点击的次数。一旦中断就重新计数。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
touchesBegan 方法在触控开始时被调用,
touchesMoved 方法在触控移动时被调用
touchesEnded 方法在触控结束时被调用
touchesCancelled 方法在系统强制结束触控时被调用
上述方法的参数:
· event 为触控动作引发的 UI 事件,只有发生了 UI 事件 UIResponder 的相应处理方法才会被调用。传递给上述方法的 UI 事件都是 “touch” 类型的,它关联了一系列 UITouch 对象来代表发生该事件时所有处于各种状态 (began, moved, ended, cancelled) 下的触控动作。
· touches 同样是一系列 UITouch 对象的** (NSSet),不过它只包含处于特定状态的触控动作。比方说调用 touchesBegan 时传递的就是所有处在 began 状态 (phase) 的 UITouch 对象。
相关类型说明:
· UIEvent 对象代表了 iPhone OS 中一个UI事件,主要有三种类型:touch,motion,remote control。
· UITouch对象代表一根手指在某一个UI事件发生时刻的动作,它有四种状态(phase),began, moved, ended, cancelled。
· 触控动作会引发 touch 类的UI事件,而这个 UI 事件中也就包括了一系列 UITouch 对象来记录当时的所有手指的动作。当 touch 类型的 UI 事件发生的时候会被交给 UIResponder 处理,继而调用上述四个方法。
OpenGL ES 程序模板中的多点触控处理
· OES 程序模板中的类继承关系为 EAGLView :UIView :UIResponder
· 在 EAGLView 类中实现了上述四个方法,并分别在其中用不同的标签调用 ESRenderer 的 view 方法。
· ESRenderer 协议(就是接口)中提供了四个同名同参的 view 方法,用标签来区分调用:
@optional
- (void)view:(UIView*)view touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)view:(UIView*)view touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)view:(UIView*)view touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
- (void)view:(UIView*)view touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;
· 实现了 ESRenderer 接口的 ES1Renderer 和 ES2Renderer 最终在 View 方法中负责触控事件的处理。
获取各种触控信息的方法
获取触控对象(UITouch)
· 通过 event 参数获取 UITouch **
//所有关联的UITouch
- (NSSet *)allTouches;
//指定窗口的UITouch
- (NSSet *)touchesForWindow:(UIWindow *)window;
//指定View上的UITouch
- (NSSet *)touchesForView:(UIView *)view;
- (NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2);
· touches 参数本身就是处在当前状态的UITouch**
· 从 touches **中随机获取一个UITouch
[touches anyObject];
UITouch 对象的各种属性
· timestamp 这个触控对象最后改变的时间戳。 用从系统启动到现在的时间来表示,float类型。开始时和移动时会更新。
· tapCount 连续点击的次数。一旦中断就重新计数。
本文详细介绍了iOS中UIView如何处理多点触控事件,包括touchesBegan、touchesMoved、touchesEnded和touchesCancelled等方法的作用及参数解析。同时,还介绍了如何在OpenGLES程序模板中实现这些触控事件的处理。

被折叠的 条评论
为什么被折叠?



