iOS事件传递
2015-05-15 08:37
141 查看
- (void)creatViews {
//frame 坐标都是 相对于父视图坐标系的坐标
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 200, 100)];
//设置背景颜色
redView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 200, 200, 100)];
label.text = @"label";
label.backgroundColor = [UIColor greenColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 0, 100, 50);
[button setTitle:@"点我" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
开启 用户交互
label.userInteractionEnabled = YES;
/*
UIButton 如果要能够接收用户的点击事件,必须保证父视图能够和用户交互,父视图的 userInteractionEnabled 必须 是YES
UILabel UIImageView 默认都是不能和用交互的 userInteractionEnabled 默认都是NO,如果要和用户交互那么必须要打开
*/
//按钮粘贴到label上
[label addSubview:button];
//把redView粘到window上--》确定了父子视图的关系
//_window是父视图 redView是_window的子视图
[self.window addSubview:redView];
[self.window addSubview:label];
[label release];
[redView release];
}
- (void)btnClick:(UIButton *)button {
NSLog(@"click");
}
- (void)creatView2 {
//frame 坐标都是 相对于父视图坐标系的坐标
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 200, 100)];
//设置背景颜色
redView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, 200, 100)];
label.text = @"label";
label.backgroundColor = [UIColor greenColor];
//label.userInteractionEnabled = YES;
/*
iOS事件传递
手指触摸屏幕之后,这时会把这个点击事件从屏幕开始向下传递,直到遇到能和用户交互的控件(userInteractionEnabled = YES),找到之后 如果这个控件有处理事件的函数,那么就直接执行,否则不处理。 如果控件不能和用户交互(userInteractionEnabled = NO)那么 会把这个点击事件向下进行传递,直到找到能交互的控件
UIView 默认userInteractionEnabled = YES
UILabel 和UIImageView 默认userInteractionEnabled = NO
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 0, 100, 50);
[button setTitle:@"点我" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//按钮粘贴到label上
[redView addSubview:button];
//把redView粘到window上--》确定了父子视图的关系
//_window是父视图 redView是_window的子视图
[self.window addSubview:redView];
[self.window addSubview:label];
[label release];
[redView release];
}
//frame 坐标都是 相对于父视图坐标系的坐标
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 200, 100)];
//设置背景颜色
redView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 200, 200, 100)];
label.text = @"label";
label.backgroundColor = [UIColor greenColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 0, 100, 50);
[button setTitle:@"点我" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
开启 用户交互
label.userInteractionEnabled = YES;
/*
UIButton 如果要能够接收用户的点击事件,必须保证父视图能够和用户交互,父视图的 userInteractionEnabled 必须 是YES
UILabel UIImageView 默认都是不能和用交互的 userInteractionEnabled 默认都是NO,如果要和用户交互那么必须要打开
*/
//按钮粘贴到label上
[label addSubview:button];
//把redView粘到window上--》确定了父子视图的关系
//_window是父视图 redView是_window的子视图
[self.window addSubview:redView];
[self.window addSubview:label];
[label release];
[redView release];
}
- (void)btnClick:(UIButton *)button {
NSLog(@"click");
}
- (void)creatView2 {
//frame 坐标都是 相对于父视图坐标系的坐标
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 200, 100)];
//设置背景颜色
redView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, 200, 100)];
label.text = @"label";
label.backgroundColor = [UIColor greenColor];
//label.userInteractionEnabled = YES;
/*
iOS事件传递
手指触摸屏幕之后,这时会把这个点击事件从屏幕开始向下传递,直到遇到能和用户交互的控件(userInteractionEnabled = YES),找到之后 如果这个控件有处理事件的函数,那么就直接执行,否则不处理。 如果控件不能和用户交互(userInteractionEnabled = NO)那么 会把这个点击事件向下进行传递,直到找到能交互的控件
UIView 默认userInteractionEnabled = YES
UILabel 和UIImageView 默认userInteractionEnabled = NO
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 0, 100, 50);
[button setTitle:@"点我" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//按钮粘贴到label上
[redView addSubview:button];
//把redView粘到window上--》确定了父子视图的关系
//_window是父视图 redView是_window的子视图
[self.window addSubview:redView];
[self.window addSubview:label];
[label release];
[redView release];
}
相关文章推荐
- 史上最详细的iOS之事件的传递和响应机制-实践篇
- 史上最详细的iOS之事件的传递和响应机制-原理篇
- iOS事件传递机制(一)
- IOS 事件响应及传递过程详解
- iOS 响应者链事件传递和分发
- iOS开发之事件传递响应链
- IOS 事件传递说明
- iOS事件的传递过程及其基本原理简单的解释
- iOS事件传递之hitTest:withEvent
- <iOS>响应事件传递, nextResponder研究
- IOS 应用事件的传递分析
- <iOS>响应事件传递, nextResponder研究
- 史上最详细的iOS之事件的传递和响应机制
- IOS——触摸事件 视图检测和事件传递
- 史上最详细的iOS之事件的传递和响应机制-原理篇
- 【iOS开发-88】事件传递原理解释哪个控件处理事件以及响应者链条的介绍
- ios事件传递
- iOS中的事件传递和响应者链条
- iOS事件的分发传递
- IOS的事件传递