文章搬運來源: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
複製代碼