加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 为什么UIButton的exclusiveTouch属性默认设置为YES?

发布时间:2020-12-14 19:53:39 所属栏目:百科 来源:网络整理
导读:我确信有很多理由说明为什么有人希望同时有多个按钮接受触摸.但是,我们大多数人只需要一次按下一个按钮(用于导航,用于以模态呈现的内容,呈现弹出窗口,视图等). 那么,为什么Apple默认将UIButton的exclusiveTouch属性设置为NO? 解决方法 很老的问题,但值得澄
我确信有很多理由说明为什么有人希望同时有多个按钮接受触摸.但是,我们大多数人只需要一次按下一个按钮(用于导航,用于以模态呈现的内容,呈现弹出窗口,视图等).

那么,为什么Apple默认将UIButton的exclusiveTouch属性设置为NO?

解决方法

很老的问题,但值得澄清IMO.

尽管来自Apple的非常误导性的方法文档,使用exclusiveTouch设置的视图“A”将阻止其他视图接收事件,只要A正在处理某些事件本身(例如,设置一个带有exclusiveTouch的按钮并将手指放在上面,这将阻止其他窗口中的视图与之交互,但是一旦移除exlusiveTouch项目的手指,与它们的交互将遵循通常的模式).

另一个效果是阻止视图A接收事件,只要一些其他视图与之交互(保持一个没有设置了exclusiveTouch设置的按钮,具有exclusiveTouch的按钮也将无法接收事件).

您仍然可以在视图中将一个按钮设置为exclusiveTouch并与其他人交互,而不是同时进行,因为这个简单的测试UIViewController将证明(一旦为Outlets和Actions设置了正确的IB绑定):

#import "FTSViewController.h"

@interface FTSViewController ()
- (IBAction)button1up:(id)sender;
- (IBAction)button2up:(id)sender;
- (IBAction)button1down:(id)sender;
- (IBAction)button2down:(id)sender;

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

@implementation FTSViewController

- (IBAction)button1up:(id)sender {
    NSLog(@"Button1 up");
}

- (IBAction)button2up:(id)sender {
    NSLog(@"Button2 up");
}

- (IBAction)button1down:(id)sender {
    NSLog(@"Button1 down");
}

- (IBAction)button2down:(id)sender {
    NSLog(@"Button2 down");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Guarantees that button 1 will not receive events *unless* it's the only receiver,as well as
    // preventing other views in the hierarchy from receiving touches *as long as button1 is receiving events*
    // IT DOESN'T PREVENT button2 from being pressed as long as no event is being intercepted by button1!!!
    self.button1.exclusiveTouch = YES;
    // This is the default. Set for clarity only
    self.button2.exclusiveTouch = NO;
}
@end

鉴于此,恕我直言,对于Apple来说,不为每个UIView子类设置exclusiveTouch为YES的唯一理由是它会使复杂手势的实现成为真正的PITA,包括我们已经习惯于复合的一些手势UIView子类(如UIWebView),将所选视图设置为exclusiveTouch = NO(如按钮)比在几乎所有内容上执行递归exclusiveTouch = YES更快,只是为了启用多点触控.

这样做的缺点是,在许多情况下,UIButtons和UITableViewCells(以及其他……)的反直觉行为可能会引入奇怪的错误并使测试变得更加棘手(因为它发生在我身上……就像10分钟前一样?:() .

希望能帮助到你

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读