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

UIResponder - 1

2015-09-01 16:47 453 查看
在UITableCell中的一个无意中见到一句代码:

[self becomeFirstResponder];
点进去一看,发现是UIResponder类的方法。我们来学习一下UIResponder是什么?



根据这张Cocoa的UI继承图,可以知道,UIView和UIViewController甚至UIApplication都是继承自UIResponder这个类的,说明它的重要性了吧!

第一眼印象深刻的是这:

NS_CLASS_***AILABLE_IOS(2_0) @interface UIResponder : NSObject {
  @private
}
尼玛,@private变量是空的,那你弄来干嘛呢。

关于响应链和传递方式就不多说了,来看看这个方法:

- (UIResponder*)nextResponder;


明白了响应链的相关知识相信对这个下一响应链的概念不难,我们点开注释:



我们都知道,苹果是闭源生态软件,对于实现是严格保密的,但是实现原理是讲述得很清晰的。

这个方法会返回接收者的下一响应者,如果没有下一响应者的话会返回nil。响应者类(响应者类别的根类)不会主动保存或者设置下一响应者,而默认是返回nil的,子类必须是重载这个方法来设置合适的下一响应者。1.UIView会返回管理它的UIViewController视图控制器对象或者是当没有视图控制器的时候返回它的直系父视图superView。2.UIViewController会重载这个方法来返回视图的直系父类。3.UIWindow则返回单例应用对象application object 4.UIApplication
则返回nil。 所以响应链是构建视图层次的时候生成的,这个时候生成的话效率最高。

这个图可以很好地总结:


(转自Reference 3 :最后系列文章)

- (BOOL)becomeFirstResponder;
- (BOOL)resignFirstResponder;
- (BOOL)isFirstResponder;
这三个方法也是非常灵活好用的,功能就不详述了。

接下来是重头戏,还记得UIEvent中iOS的事件是分为几种类型? 分别是触碰事件、运动事件和远程控制事件。而作为UIResponder如何将响应者和接收处理事件的能力绑定在一起呢?答案就是一系列强大的方法:

- (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;

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_***AILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_***AILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_***AILABLE_IOS(3_0);

- (void)remoteControlReceivedWithEvent:(UIEvent *)event NS_***AILABLE_IOS(4_0);


参考自:

Reference 1: http://southpeak.github.io/blog/2015/03/07/uiresponder/?utm_source=tuicool
Reference 2: http://www.cnblogs.com/kenshincui/p/3950646.html#touch
Reference 3: http://www.cnblogs.com/syxchina/archive/2012/10/14/2723541.html
Reference 4: http://southpeak.github.io/blog/2015/03/07/uiresponder/?utm_source=tuicool
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: