本文共 681 字,大约阅读时间需要 2 分钟。
自定义一个继承自UIView的子类
重写子类的hit-test
#import "TestView.h"
@implementation TestView
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (!view) {
if (self.subviews.count > 0) {
for (UIView *v in self.subviews) {
CGPoint tempPoi = [v convertPoint:point fromView:self];
if (CGRectContainsPoint(v.bounds, tempPoi)) {
view = v;
break;
}
}
}
}
return view;
}
@end
说一下[v convertPoint:point fromView:self];这句代码的含义
首先上面代码hit-test方法中的point通过打印可以知道是 对于浅蓝色的自定义view而言的
其次这句代码做的事是:把self上面的点转换到红色button上的点,即转换完毕之后点的x,y是相对于红色button的左上角而言的
举个例子:比如当点击button的时候,point是 (x:19,y:131)转换完毕tempPoi是(x:19,y:31)(注:浅蓝色view宽高均为100,红色button宽高均为50)