UIView事件接收

2016-05-22  本文已影响373人  Barry_小闪

设置控件是否可以接收事件

 //YES:可以进行用户交互
 //NO:不可以进行用户交互
 imageView.userInteractionEnabled = YES;

 关闭按钮的用户交互
 button.userInteractionEnabled = NO;

1.父视图不能接收事件,则子视图无法接受事件。

  UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(-30, -30, 80, 80)];
  button.backgroundColor = [UIColor redColor];
  [button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
  [_window addSubview:button];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.backgroundColor = [UIColor yellowColor];
[_window addSubview:imageView];

  UIImageView默认不可以接收事件,所以添加到UIImageView上面的子视图不能接收事件
  [imageView addSubview:button];

2.子视图超出父视图的部分,不能接收事件

3.同一个父视图下,最上面的视图,首先遭遇事件,如果能够响应,就不向下传递事件。如果不能接收,事件向下传递。

 UIButton * button2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];  button2.backgroundColor = [UIColor purpleColor];
 [button2 addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
 [self.window addSubview:button2];
 [self.window sendSubviewToBack:button2];

  imageView.userInteractionEnabled = NO;
上一篇 下一篇

猜你喜欢

热点阅读