参考
[1]http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009541-CH1-SW1
1.事件类型
主要是 触摸事件,动作事件(甩动等),和远程控制事件三类。
typedef enum {
UIEventTypeTouches,
UIEventTypeMotion,
UIEventTypeRemoteControl,
} UIEventType;
2.加速器和陀螺仪
UIAccelerometer 和 UIAcceleration以后可能被废弃,所以应该使用Core Motion framework中的接口。
Notes: The UIAccelerometer andUIAcceleration classes will be deprecated in a future release, so if your application handles accelerometer events, it should transition to the Core Motion API.
In iOS 3.0 and later, if you are trying to detect specific types of motion as gestures—specifically shaking motions—you should consider handling motion events (UIEventTypeMotion) instead of using the accelerometer interfaces. If you want to receive and handle high-rate, continuous motion data, you should instead use the Core Motion accelerometer API. Motion events are described in“Shaking-Motion Events.”
3.事件响应链
简单说就是:view->viewController->supperview 如此循环。具体过程如下
If the application object cannot handle the event or message, it discards it.
4.hitTest查找被点中的view
用户点击屏幕,系统是如何查找当前被点击所在的view,如果view是隐藏的,透明的,userInteractionEnabled 为NO 都不被列入查找的目标。
系统查找响应的view是从上往下的,事件的响应链是从下往上的。
具体说明如下
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { return CGRectContainsPoint(self.bounds, point); } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if (self.hidden || !self.userInteractionEnabled || self.alpha < 0.01 || ![self pointInside:point withEvent:event]) { return nil; } else { for (UIView *subview in [self.subviews reverseObjectEnumerator]) { UIView *hitView = [subview hitTest:[subview convertPoint:point fromView:self] withEvent:event]; if (hitView) { return hitView; } } return self; } }
系统支持6中基本手势
Tapping (any number of taps) UITapGestureRecognizer
Pinching in and out (for zooming a view)UIPinchGestureRecognizer
Panning or draggingUIPanGestureRecognizer
Swiping (in any direction)UISwipeGestureRecognizer
Rotating (fingers moving in opposite directions)UIRotationGestureRecognizer
Long press (also known as “touch and hold”)UILongPressGestureRecognizer
使用的方法就是 创建对应实例,然后 addGestureRecognizer 加入对应的view即可,当识别出来则调用响应的回掉函数。
如果要实现自己的手势识别,继承 UIGestureRecognizer,重写以下四个方法,自己在里面实现手势识别算法。具体看文档吧。
- (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;
其他没什么特别的了。