在Event Handling.pdf中看到下面一段话,划线的那句话无法理解。难道TouchesBegan是用来给receiver传UIEvent中UITouch的?帮忙解释一下(顺带着解释Ended,Moved,Cancelled)
而且TouchesBegan在UIResponder和UIGestureRecognizer中都有,那这两者又有什么区别?
还有当我把TouchesBegan写在controller中又是什么作用?
还有touchesBegan中调用父类的实现又是什么目的?[super touchesBegan..];
而且TouchesBegan在UIResponder和UIGestureRecognizer中都有,那这两者又有什么区别?
还有当我把TouchesBegan写在controller中又是什么作用?
还有touchesBegan中调用父类的实现又是什么目的?[super touchesBegan..];
touchBegan这四个方法虽然是继承得来的,但是表现的更像是勾子方法,一般不需要在子view的这些方法里调用super。
写在Controller里一点用都没有,事件是在view上传递的,Controller和view的关系应该是一种委托关系,即双方解耦:想替换逻辑,就换Controller;想换UI,就换view。
如果子view不想触发这个事件,应该调用self.nextResponder,不然事件就断了,父view以为你能处理,就不会继续下发下去了。
才看到你的邮件,真是不好意思
Controller 之所以会响应 touches方法,是因为 Controller 作了 view 的 delegate,你可以这样测试:把 self.view 实例化成一个自定义的 view:@interface MyView : UIView@end@implementation MyView- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"view began");
}@end你会发现 Controller 的 touches 方法不会再被调用了,除非再加上:- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesEnded:touches withEvent:event];
}勾子方法不太好解释,简而言之就是告诉你当前系统的状态,并可以做出一些控制,如事件的传递等,但是它本身并不属于逻辑。