您的位置:首页 > 产品设计 > UI/UE

Event Handling Guide for iOS

2012-11-17 16:38 573 查看
参考

[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 如此循环。具体过程如下

The hit-test view or first responder passes the event or message to itsview controller if it has one; if the view doesn’t have a view controller, it passes the event or message to its superview.
If a view or its view controller cannot handle the event or message, it passes it to thesuperview of the view.
Each subsequent superview in the hierarchy follows the pattern described in the first two steps if it cannot handle the event or message.
The topmost view in the view hierarchy, if it doesn’t handle the event or message, passes it to thewindow object for handling.
The UIWindow object, if it doesn’t handle the event or message, passes it to thesingleton
application object.

If the application object cannot handle the event or message, it discards it.

4.hitTest查找被点中的view
用户点击屏幕,系统是如何查找当前被点击所在的view,如果view是隐藏的,透明的,userInteractionEnabled 为NO 都不被列入查找的目标。
系统查找响应的view是从上往下的,事件的响应链是从下往上的。
具体说明如下

Touch events. The window object uses hit-testing and the responder chain to find the view to receive the touch event. In hit-testing, a window callshitTest:withEvent:
on the top-most view of the view hierarchy; this method proceeds by recursively callingpointInside:withEvent:
on each view in the view hierarchy that returnsYES, proceeding down the hierarchy until it finds the subview within whose bounds the touch took place. That view becomes the hit-test 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;
}
}


5.手势识别

系统支持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;

其他没什么特别的了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: