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

Mig*_*elB 8 iphone xcode uibutton ipad

我确信有很多理由说明为什么有人希望同时有多个按钮接受触摸.但是,我们大多数人只需要一次按下一个按钮(用于导航,用于呈现模态,呈现弹出窗口,视图等).

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

Ric*_*k77 9

很老的问题,但值得澄清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
Run Code Online (Sandbox Code Playgroud)

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

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

希望能帮助到你

  • @jjxtra 如何做到这一点,重写哪个方法? (2认同)