iOS超出父控件范围无法点击问题解决
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>场景</li><li>实现思路</li><li>完整代码</li><ul class="second_class_ul"><li>botView代码</li></ul></ul></div><p class="maodian"></p><h2>场景</h2><p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202306/2303062510343409.jpg" /></p>
<p>橙色view添加在蓝色view上,满足点击超出蓝色view部分可以响应事件</p>
<p class="maodian"></p><h2>实现思路</h2>
<p>重写底部蓝色view的hitTest方法,从最上层依次遍历子控件,判断触摸点是否在子控件上,在的话就返回子控件的hitTest方法,不在就返回self</p>
<p class="maodian"></p><h2>完整代码</h2>
<div class="jb51code"><pre class="brush:cpp;">#import "ViewController.h"
#import "BotView.h"
@interface ViewController ()
@property(strong, nonatomic) BotView *botView;
@property(strong, nonatomic) UIView *topView;
@end
@implementation ViewController
- (void)viewDidLoad {
;
self.view.backgroundColor = ;
// Do any additional setup after loading the view.
self.botView = ;
self.topView = ;
;
;
self.botView.frame = CGRectMake(100, 100, 100, 100);
self.topView.frame = CGRectMake(0, 0, 50, 200);
self.botView.backgroundColor = ;
self.topView.backgroundColor = ;
UITapGestureRecognizer *tap = [initWithTarget:self action:@selector(topViewClick:)];
;
}
- (void)topViewClick:(UITapGestureRecognizer *)tap {
NSLog(@"点击了顶部view");
}
@end</pre></div>
<p class="maodian"></p><h3>botView代码</h3>
<div class="jb51code"><pre class="brush:cpp;">#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;
//点击事件作用在子控件上面,返回点击点
CGPoint isPoint = ;
UIView *subv = ;
if (subv) {
return subv;
}
}
return self;
}
@end</pre></div>
<p>以上就是iOS超出父控件范围无法点击问题解决的详细内容,更多关于iOS父控件无法点击的资料请关注琼殿技术社区其它相关文章!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>iOS自定义时间滚动选择控件</li><li>iOS下拉、上拉刷新控件的封装</li><li>Android自定义控件仿ios下拉回弹效果</li><li>Android仿IOS回弹效果 支持任何控件</li><li>Android自定义控件仿iOS滑块SwitchButton</li><li>iOS 简约日历控件EBCalendarView的实现代码</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]