iOS事件传递及处理方法
iOS事件传递及处理方法。UIView继承与UIResponder,UIResponder提供四个处理方法(PS:不使用父类处理,【super之类的,会拦截事件)。
- (void)touchesBegan:(NSSet
NSLog(@"touchBegan");
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet
NSLog(@"touchMoved");
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet
NSLog(@"touchEnded");
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet
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;
}
上一篇: CAD怎么画一个吊灯的平面图?