您的位置:首页 > 产品设计 > UI/UE

UIButton按钮点击无响应(按钮的位置在父视图之外的解决方法),传递响应链

2016-10-18 22:40 543 查看
项目中碰到个bug,当前控制器的TabBar隐藏之后,在TabBar位置添加一个按钮,由于self.view的y值距离底部是49,点击按钮没有反应,特此记录下解决方法

自定义view,比如demoView

#import

@interface demoView : UIView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event ;

@end


#import "demoView.h"

@implementation demoView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
// 超出父视图的按钮tag值为10000
CGPoint tempoint = [[self viewWithTag:10000] convertPoint:point fromView:self];
if (CGRectContainsPoint([self viewWithTag:10000].bounds, tempoint))
{
view = [self viewWithTag:10000];
}
}
return view;
}

@end


在控制器中

- (void)viewDidLoad {
self.view = [[THYCClickBtnView alloc] initWithFrame:self.view.frame];
}


按钮

self.buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.buyBtn.backgroundColor = [UIColor redColor];
self.buyBtn.layer.cornerRadius = 5.f;
self.buyBtn.layer.masksToBounds = YES;
[self.buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
[self.view addSubview:self.buyBtn];
self.buyBtn.tag = 10000;
[self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_bottom).with.offset(0);
make.right.equalTo(self.view.mas_right).with.offset(-8);
make.left.equalTo(self.view.mas_left).with.offset(8);
make.height.mas_equalTo(35);

}];
[self.buyBtn addTarget:self action:@selector(didBuyNow:) forControlEvents:UIControlEventTouchUpInside];


此方法同样适用于,穿透UIvew实现下层的UIbutton 的点击事件
hitTest的作用:当在一个view上添加一个屏蔽罩,但又不影响对下面view的操作,也就是可以透过屏蔽罩对下面的view进行操作,这个函数就可以实现 hitTest的用法:将下面的函数添加到UIView的子类中.实现点击事件
其他方法的作用

IOS-- UIView中的坐标转换我用的我是换个是
// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值(这个是我用的)
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息