文章搬运来源:blog.csdn.net/Calvin_zhou…面试
做者:PGzxcmarkdown
对iOS开发感兴趣,能够看一下做者的iOS交流群:812157648,你们能够在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎你们加群,你们一块儿开车ide
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
复制代码
当事件传递给一个控件的时候就会调用oop
寻找最合适的view布局
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (self.userInteractionEnabled==NO||self.hidden==YES||self.alpha<=0.01) {
return nil;
}
if (![self pointInside:point withEvent:event]) {
return nil;
}
int count=self.subviews.count;
for (int i=count-1; i>=0; i--) {
UIView *childView=self.subviews[i];
//转换坐标系
CGPoint childPoint=[self convertPoint:point toView:childView];
UIView *fitView= [childView hitTest:childPoint withEvent:event];
if (fitView) {
return fitView;
}
}
return self;
}
复制代码
@interface GreenView : UIView
@property (nonatomic,weak) IBOutlet UIButton *button;
@end
复制代码
#import "GreenView.h"
@implementation GreenView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s",__func__);
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
//把本身的点转换为按钮坐标系上的点
CGPoint buttonPoint=[self convertPoint:point toView:_button];
if ([_button pointInside:buttonPoint withEvent:event]) {
return nil;
}
return [super hitTest:point withEvent:event];
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint buttonPoint=[self convertPoint:point toView:_button];
if ([_button pointInside:buttonPoint withEvent:event]) {
return NO;
}
return [super pointInside:point withEvent:event];
}
@end
复制代码