ios代码如何让子类视图超出父类视图的部分响应事件
答案:3 悬赏:10 手机版
解决时间 2021-04-14 06:29
- 提问者网友:最美的风景
- 2021-04-14 01:06
ios代码如何让子类视图超出父类视图的部分响应事件
最佳答案
- 五星知识达人网友:执傲
- 2021-04-14 01:32
在父视图重写方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (self.userInteractionEnabled == NO && self.alpha <= 0.01 && self.hidden == YES) {
return nil;
}
for (UIView * subview in self.subviews.reverseObjectEnumerator) {
CGPoint converP = [subview convertPoint:point fromView:self];
UIView *suitableView = [subview hitTest:converP withEvent:event];
if (resultView) {
return suitableView;
} else {
return view;
}
}
return view;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (self.userInteractionEnabled == NO && self.alpha <= 0.01 && self.hidden == YES) {
return nil;
}
for (UIView * subview in self.subviews.reverseObjectEnumerator) {
CGPoint converP = [subview convertPoint:point fromView:self];
UIView *suitableView = [subview hitTest:converP withEvent:event];
if (resultView) {
return suitableView;
} else {
return view;
}
}
return view;
}
全部回答
- 1楼网友:罪歌
- 2021-04-14 03:27
//重写hit让超出self范围的子视图能响应点击时间
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (self.userInteractionEnabled == NO && self.alpha<=0.01 && self.hidden == YES) {
return nil;
}
//根据返回的view是否为空判断点击的是否是在父视图的范围内,null则表示不再范围内,否则返回自己本身。
UIView *view = [super hitTest:point withEvent:event];
if (!view) {
for (UIView *subView in self.subviews) {
CGPoint converPoint = [subView convertPoint:point fromView:self];
//点是否在相应的视图上
if (CGRectContainsPoint(subView.bounds, converPoint)) {
//相应的视图处理事件并返回
return [subView hitTest:converPoint withEvent:event] ;
}
}
}
return view;
}
- 2楼网友:我住北渡口
- 2021-04-14 02:59
重写父视图的hitTest:(CGPoint)point withEvent:(UIEvent *)event方法
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯