iOS button被view遮住,想点击怎么破
2015-07-01 15:39
483 查看
UIView的提供了这个方法:- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
1.创建CustomView继承自UIView :
CustomView.h
@property (strong,nonatomic)NSArray * passthroughViews;
@property (nonatomic)BOOL testHits;
-(BOOL) isPassthroughView: (UIView*) view;
CustomView.m
-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if(self.testHits){
return nil;
}
if(!self.passthroughViews
|| (self.passthroughViews && self.passthroughViews.count == 0)){
return self;
} else {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self) {
self.testHits = YES;
CGPoint superPoint = [self.superview convertPoint:point fromView:self];
UIView *superHitView = [self.superview hitTest:superPoint withEvent:event];
self.testHits = NO;
if ([self isPassthroughView:superHitView]) {
hitView = superHitView;
}
}
return hitView;
}
}
- (BOOL)isPassthroughView:(UIView *)view {
if (view == nil) {
return NO;
}
if ([self.passthroughViews containsObject:view]) {
return YES;
}
return [self isPassthroughView:view.superview];
}
2.创建ViewController继承自UIViewController :
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.passthroughButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.passthroughButton setTitle:@"Passthrough" forState:UIControlStateNormal];
[self.view addSubview:self.passthroughButton];
self.passthroughButton.frame = CGRectMake(20, 50, 120, 28);
[self.passthroughButton addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(80, 10, 300, 200)];
customView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.5];
//customView.passthroughViews = [NSArray arrayWithObject:self.passthroughButton];
[self.view addSubview:customView];
}
1.创建CustomView继承自UIView :
CustomView.h
@property (strong,nonatomic)NSArray * passthroughViews;
@property (nonatomic)BOOL testHits;
-(BOOL) isPassthroughView: (UIView*) view;
CustomView.m
-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if(self.testHits){
return nil;
}
if(!self.passthroughViews
|| (self.passthroughViews && self.passthroughViews.count == 0)){
return self;
} else {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self) {
self.testHits = YES;
CGPoint superPoint = [self.superview convertPoint:point fromView:self];
UIView *superHitView = [self.superview hitTest:superPoint withEvent:event];
self.testHits = NO;
if ([self isPassthroughView:superHitView]) {
hitView = superHitView;
}
}
return hitView;
}
}
- (BOOL)isPassthroughView:(UIView *)view {
if (view == nil) {
return NO;
}
if ([self.passthroughViews containsObject:view]) {
return YES;
}
return [self isPassthroughView:view.superview];
}
2.创建ViewController继承自UIViewController :
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.passthroughButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.passthroughButton setTitle:@"Passthrough" forState:UIControlStateNormal];
[self.view addSubview:self.passthroughButton];
self.passthroughButton.frame = CGRectMake(20, 50, 120, 28);
[self.passthroughButton addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(80, 10, 300, 200)];
customView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.5];
//customView.passthroughViews = [NSArray arrayWithObject:self.passthroughButton];
[self.view addSubview:customView];
}
相关文章推荐
- iOS 设置最后一行的分割线边距为零
- iOS 截取屏幕当前显示的图片(可按范围截取)
- iOS开发_统计xcode代码行数
- iOS语言中的KVO机制
- 斯坦福大学ios公开课课程源代码下载地址
- iOS代码文档化,比注释更高级!
- 查看iOS错误日志
- iOS statusBar 状态栏定制
- IOS 给类目添加属性
- iOS绘图
- iOS8学习笔记2--autolayout
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- ios 本地通知
- iOS 6 自动布局 入门 2
- 截取字符串
- iOS view的frame和bounds之区别(位置和大小)
- iOS 项目如何实现版本的更新?
- IOS 6 自动布局 入门-1
- h5 与 IOS 、安卓 配合的要注意要几点
- ios json数据解析