美文网首页
iOS 事件机制

iOS 事件机制

作者: 鄙人哈哈哈哈5871 | 来源:发表于2016-08-14 20:51 被阅读9次

原文链接 http://www.cnblogs.com/Quains/category/496428.html

一、处理机制界面响应消息机制分两块,(1)首先在视图的层次结构里找到能响应消息的那个视图。(2)然后在找到的视图里处理消息。

(1)的过程是从父view到子view查找(2)是从子view往父view传递

当用户点击屏幕时,会产生一个触摸事件,系统会将该事件加入到一个由UIApplication管理的事件队列中•UIApplication会从事件队列中取出最前面的事件进行分发以便处理,

处理原理如下:

• 当用户点击屏幕时,会产生一个触摸事件,系统会将该事件加入到一个由UIApplication管理的事件队列中

• UIApplication会从事件队列中取出最前面的事件进行分发以便处理,通常,先发送事件给应用程序的主窗口(UIWindow)

• 主窗口会调用hitTest:withEvent:方法在视图(UIView)层次结构中找到一个最合适的UIView来处理触摸事件

(hitTest:withEvent:其实是UIView的一个方法,UIWindow继承自UIView,因此主窗口UIWindow也是属于视图的一种)

• hitTest:withEvent:方法大致处理流程是这样的:

首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内:

若pointInside:withEvent:方法返回NO,说明触摸点不在当前视图内,则当前视图的hitTest:withEvent:返回nil

若pointInside:withEvent:方法返回YES,说明触摸点在当前视图内,则遍历当前视图的所有子视图(subviews),调用子视图的hitTest:withEvent:方法重复前面的步骤,子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图的hitTest:withEvent:方法返回非空对象或者全部子视图遍历完毕:

若第一次有子视图的hitTest:withEvent:方法返回非空对象,则当前视图的hitTest:withEvent:方法就返回此对象,处理结束

若所有子视图的hitTest:withEvent:方法都返回nil,则当前视图的hitTest:withEvent:方法返回当前视图自身(self)

• 最终,这个触摸事件交给主窗口的hitTest:withEvent:方法返回的视图对象去处理。

拿到这个UIView后,就调用该UIView的touches系列方法。

http://images.cnitblog.com/blog/497279/201303/06022409-75bfb59830bb45c3899de47f77721214.png

二、应用实例

【需求】是:界面如下,

Window

    -ViewA

          -ButtonA

           -ViewB

                 -ButtonB

层次结构:ViewB完全盖住了ButtonA,ButtonB在ViewB上,现在需要实现1)ButtonA和ButtonB都能响应消息 2)ViewA也能收到ViewB所收到的touches消息 3)不让ViewB(ButtonB)收到消息。

(1)

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

// 当touch point是在_btn上,则hitTest返回_btn

CGPoint btnPointInA = [_btn convertPoint:point fromView:self];

if([_btn pointInside:btnPointInA withEvent:event]) {

return_btn;

}

// 否则,返回默认处理

return[superhitTest:point withEvent:event];

}

(2)

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

{

NSLog(@"B - touchesBeagan..");

// 把事件传递下去给父View或包含他的ViewController

[self.nextResponder touchesBegan:touches withEvent:event];

}

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

{

NSLog(@"B - touchesCancelled..");

// 把事件传递下去给父View或包含他的ViewController

[self.nextResponder touchesBegan:touches withEvent:event];

}

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

{

NSLog(@"B - touchesEnded..");

// 把事件传递下去给父View或包含他的ViewController

[self.nextResponder touchesBegan:touches withEvent:event];

}

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

{

NSLog(@"B - touchesMoved..");

// 把事件传递下去给父View或包含他的ViewController

[self.nextResponder touchesBegan:touches withEvent:event];

}

// 福view

#pragma mark - touches

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

{

NSLog(@"A - touchesBeagan..");

}

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

{

NSLog(@"A - touchesCancelled..");

}

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

{

NSLog(@"A - touchesEnded..");

}

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

{

NSLog(@"A - touchesMoved..");

}

(3)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

{

// 本View不响应用户事件

returnNO;

}

相关文章

网友评论

      本文标题:iOS 事件机制

      本文链接:https://www.haomeiwen.com/subject/ndoysttx.html