重寫父控件中的方法:(兩個方法任選其一:)
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
具體實現(xiàn):
- 方法一:
//返回需要響應事件的view位置
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
//轉(zhuǎn)換坐標系
CGPoint newPoint = [self.thirdBtn convertPoint:point fromView:self];
//判斷觸摸點是否在button上
if (CGRectContainsPoint(self.thirdBtn.bounds, newPoint)) {
view = self.thirdBtn;
}
//NSLog(@"point:%@ : newPoint:%@",NSStringFromCGPoint(point),NSStringFromCGPoint(newPoint));
}
return view;
}
- 方法二:
//當point在父控件和子控件包含的區(qū)域時郎哭,應該返回YES,否則為NO积蔚;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
NSLog(@"point:%@",NSStringFromCGPoint(point));
if (!CGRectContainsPoint(self.bounds, point)) {
//轉(zhuǎn)換坐標系
CGPoint newPoint = [self convertPoint:point toView:self.secondBtn];
//NSLog(@"newPoit:%@",NSStringFromCGPoint(newPoint));
//判斷觸摸點是否在secondBtn上
if (CGRectContainsPoint(self.secondBtn.bounds, newPoint)) {
return YES;
}
return NO;
}
return YES;
}
難點:
- 判斷一個點是否在某個范圍內(nèi)
CGRectContainsPoint(Rect, Point); //判斷Rect中是否包含Point
CGRectContainsRect(Rect1,Rect2);//判斷Rect1中是否包含Rect2
- 實現(xiàn)對控制點坐標系的轉(zhuǎn)換
`view1:視圖一` `view2:視圖二`
`point:view1坐標系中的點` `newPoint:新得到view2坐標系中的點`
//將point從view1的坐標系中轉(zhuǎn)換到view2的坐標系中得到newPoint
CGPoint newPoint = [view2 convertPoint:point fromView:view1];
CGPoint newPoint = [view1 convertPoint:point toView:view2];
類似方法:
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者