iOS事件传递

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

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值