场景
橙色view添加在蓝色view上,满足点击超出蓝色view部分可以响应事件
实现思路
重写底部蓝色view的hittest方法,从最上层依次遍历子控件,判断触摸点是否在子控件上,在的话就返回子控件的hittest方法,不在就返回self
完整代码
#import "viewcontroller.h" #import "botview.h" @interface viewcontroller () @property(strong, nonatomic) botview *botview; @property(strong, nonatomic) uiview *topview; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor whitecolor]; // do any additional setup after loading the view. self.botview = [botview new]; self.topview = [uiview new]; [self.view addsubview:self.botview]; [self.botview addsubview:self.topview]; self.botview.frame = cgrectmake(100, 100, 100, 100); self.topview.frame = cgrectmake(0, 0, 50, 200); self.botview.backgroundcolor = [uicolor linkcolor]; self.topview.backgroundcolor = [uicolor orangecolor]; uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(topviewclick:)]; [self.topview addgesturerecognizer:tap]; } - (void)topviewclick:(uitapgesturerecognizer *)tap { nslog(@"点击了顶部view"); } @end
botview代码
#import "botview.h" @implementation botview - (uiview *)hittest:(cgpoint)point withevent:(uievent *)event{ int count = (int)self.subviews.count; for (int i=count-1; i>=0; i--) { uiview *subview = self.subviews[i]; //点击事件作用在子控件上面,返回点击点 cgpoint ispoint = [self convertpoint:point toview:subview]; uiview *subv = [subview hittest:ispoint withevent:event]; if (subv) { return subv; } } return self; } @end
以上就是ios超出父控件范围无法点击问题解决的详细内容,更多关于ios父控件无法点击的资料请关注代码网其它相关文章!
发表评论