UIView独占响应事件

简介:

exclusiveTouch

A Boolean value that indicates whether the receiver handles touch events exclusively.
Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO.

一个布尔值,用来标示一个view独占触摸事件.

当把一个view中的exclusiveTouch设置成YES时,会致使这个window屏蔽掉其他的view触发响应事件.默认值是NO.

 

不用说就知道他的用处了,你不知道怎么回事么?以后你会懂的,测试人员给你提bug的时候你就知道了,上例子:

#import "RootViewController.h"

@interface RootViewController ()

@property (nonatomic, strong) UIButton   *button1;
@property (nonatomic, strong) UIButton   *button2;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:_button1];
    _button1.backgroundColor = [UIColor redColor];
    [_button1 addTarget:self
                 action:@selector(buttonsEvent:)
       forControlEvents:UIControlEventTouchUpInside];
    
    _button1.exclusiveTouch = YES;

    _button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
    [self.view addSubview:_button2];
    _button2.backgroundColor = [UIColor greenColor];
    [_button2 addTarget:self
                 action:@selector(buttonsEvent:)
       forControlEvents:UIControlEventTouchUpInside];
    
    
    _button2.exclusiveTouch = YES;
}

- (void)buttonsEvent:(id)sender
{
    if (_button1 == sender)
    {
        NSLog(@"1");
    }
    
    if (_button2 == sender)
    {
        NSLog(@"2");
    }
}

@end

 

 

 

 

 

 

 

目录
相关文章
|
11月前
|
Web App开发 JavaScript 算法
深入解析 EventLoop 和浏览器渲染、帧动画、空闲回调的关系
关于 Event Loop 的文章很多,但是有很多只是在讲「宏任务」、「微任务」
|
Web App开发 JavaScript 前端开发
页面运行中事件频繁触发会阻塞页面吗?
之前看`防抖`和`节流`的时候,看到短时间内大量的事件触发会引起浏览器卡死,浪费浏览器性能,那么为什么事件触发会引起阻塞页面的情况呢?引起页面阻塞的原因真的是因为事件触发太多了吗?
SwiftUI—如何实现对视图显示和消失事件的监听
SwiftUI—如何实现对视图显示和消失事件的监听
590 0
SwiftUI—如何实现对视图显示和消失事件的监听
非UI线程下页面处理:view的postInvalidate和post对消息处理的差异化
我们知道view有一系列post方法,用于在非UI线程中发出一些页面处理。view还有另外一个postInvalidate方法,同样在非UI线程中发起重绘。 同样是在非UI线程向UI线程发出消息,但是这里面有很大的区别。
180 0