iOS超出父控件范圍無法點(diǎn)擊問題解決
更新時間:2023年06月26日 17:24:29 作者:螢火駐守心間
這篇文章主要介紹了iOS超出父控件范圍無法點(diǎn)擊問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
場景
橙色view添加在藍(lán)色view上,滿足點(diǎn)擊超出藍(lán)色view部分可以響應(yīng)事件
實現(xiàn)思路
重寫底部藍(lán)色view的hitTest方法,從最上層依次遍歷子控件,判斷觸摸點(diǎn)是否在子控件上,在的話就返回子控件的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(@"點(diǎn)擊了頂部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]; //點(diǎn)擊事件作用在子控件上面,返回點(diǎn)擊點(diǎn) CGPoint isPoint = [self convertPoint:point toView:subView]; UIView *subv = [subView hitTest:isPoint withEvent:event]; if (subv) { return subv; } } return self; } @end
以上就是iOS超出父控件范圍無法點(diǎn)擊問題解決的詳細(xì)內(nèi)容,更多關(guān)于iOS父控件無法點(diǎn)擊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何用IOS調(diào)用WebService(SOAP接口)
這篇文章主要介紹了如何用IOS調(diào)用WebService(SOAP接口),需要的朋友可以參考下2015-07-07