很老的问题,但值得澄清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分钟前一样?:() .
希望能帮助到你