https://www.jianshu.com/p/2e074db792ba
以上是最详细的博客,讲的很细致,不再赘述。一下为精华补充部分
UIView继承与UIResponder,UIResponder提供四个处理方法(PS:不使用父类处理,【super之类的,会拦截事件)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touchBegan");
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touchMoved");
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touchEnded");
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touchCancelled");
[super touchesCancelled:touches withEvent:event];
}
UIView进行事件分发处理时,两个主要的方法
//此方法用于确定该事件是不是本View内部,也可以人工篡改不拦截事件。(一个view能处理事件的充分必要条件是以下两个都是yes,而且view.userInterfaceEnable = YES 才可以)
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
return YES;
}
//此方法用来确定本View的各个子View中,哪个处理event合适,如果没有合适的子view,就会返回本类
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
NSArray * views = self.subviews;
for (UIView * view in views) {
if ([view isKindOfClass:[ButtonC class]]) {
return view;
}
}
return nil;
}