iOS 事件穿透
2015-10-15 17:44 xiangjune 阅读(366) 评论(0) 编辑 收藏 举报
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 100, 100, 50);
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"click me" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
MyView1 *view = [[MyView1 alloc] initWithTestFrame:CGRectMake(100, 100, 200, 200)];
[self.view addSubview:view];
}
// ---------------------------------------------自定义VIEW
#import "MyView1.h"
@implementation MyView1
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
UIButton *btn;
-(id)initWithTestFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
if(self)
{
self.backgroundColor = [UIColor blueColor];
[self drawView];
// self.userInteractionEnabled = NO;
}
return self;
}
-(void) drawView
{
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 50, 30);
btn.backgroundColor = [UIColor blackColor];
[btn setTitle:@"btn" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect rect = btn.frame;
BOOL containsPoint = CGRectContainsPoint(rect, point);
if(containsPoint)
{
self.userInteractionEnabled = YES;
}
else
{
self.userInteractionEnabled = NO;
}
UIView *touchedView = [super hitTest:point withEvent:event];
return touchedView;
}
-(void)click
{
NSLog(@"black...");
}
@end