您的位置:首页 > 移动开发 > IOS开发

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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: