iphone - 为什么 UIButton 的 ExclusiveTouch 属性默认不设置为 YES?

标签 iphone xcode ipad uibutton

我确信有人希望有多个按钮同时接受触摸的原因有很多。然而,我们大多数人一次只需要按下一个按钮(用于导航、以模态方式呈现某些内容、呈现弹出框、 View 等)。

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

最佳答案

非常老的问题,但在我看来值得澄清。

尽管Apple提供了非常误导性的方法文档,设置了exclusiveTouch的 View “A”将阻止其他 View 接收事件只要A本身正在处理某些事件(例如,设置一个带有 ExclusiveTouch 的按钮并将手指放在其上,这将阻止窗口中的其他 View 与之交互,但一旦移除了 exlusiveTouch-item 中的手指,与它们的交互将遵循通常的模式)。

另一个效果是,只要与其他 View 进行交互,就阻止 View A 接收事件(保持按下未设置 ExclusiveTouch 的按钮,并且具有 ExclusiveTouch 的按钮也将无法接收事件)。

您仍然可以在 View 中将按钮设置为 ExclusiveTouch 并与其他按钮进行交互,只是不能同时进行,正如这个简单的测试 UIViewController 将证明的那样(一旦为 Outlet 和 Actions 设置了 IB 中的正确绑定(bind)) :

#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)中,因为将选定 View 设置为 ExclusiveTouch=NO(如按钮)比在几乎所有内容上执行递归 ExclusiveTouch=YES 来启用多点触控要快。

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

希望对你有帮助

关于iphone - 为什么 UIButton 的 ExclusiveTouch 属性默认不设置为 YES?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6507794/

相关文章:

iphone - 在 iOS(某些版本)中切换声音音量时不一致的 OpenAL 爆音伪像

iphone - 从 iPhone 应用程序访问现有语音笔记

iphone - 如何实现连续拖拽菜单效果?

ios - Xcode 在通过 QuickTime 录制屏幕时失去与 iPad 的连接

ios - 没有兼容模式的 iPad 上的 iPhone 应用

iOS9:在 UIWebView 中断应用程序中选择字段

ios - 如何以编程方式使视频静音

iphone SDK检测Wifi和运营商网络

ios - 在 TabBarController 项中有一个 NavigationController

xcode - 在 Xcode 6 中创建新的 Assets 目录/图像集