欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS事件传递及处理方法

程序员文章站 2023-12-25 00:00:09
iOS事件传递及处理方法。UIView继承与UIResponder,UIResponder提供四个处理方法(PS:不使用父类处理,【super之类的,会拦截事件)。 - (voi...

iOS事件传递及处理方法。UIView继承与UIResponder,UIResponder提供四个处理方法(PS:不使用父类处理,【super之类的,会拦截事件)。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchBegan");
[super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchMoved");
[super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchEnded");
[super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)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;
}

上一篇:

下一篇: