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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6507794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:22:04  来源:igfitidea点击:

Why isn't UIButton's exclusiveTouch property set to YES by default?

iphonexcodeipaduibutton

提问by MiguelB

I'm sure there are a lot of reasons why someone would like to have more than one button accept touches at the same time. However, most of us only need one button to be pressed at one time (for navigation, for something to be presented modally, to present a popover, a view, etc.).

我敢肯定,有人希望让多个按钮同时接受触摸的原因有很多。然而,我们大多数人一次只需要按下一个按钮(用于导航、以模态呈现的东西、呈现弹出框、视图等)。

So, why would Apple set the exclusiveTouchproperty of UIButtonto NO by default?

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

回答by Rick77

Very old question, but deserves clarification IMO.

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

Despite the very misleading method documentation from Applea view "A" with exclusiveTouch set will prevent other views from receiving events so long as A is processing some event itself(e.g. set a button with exclusiveTouch and put a finger on it, this will prevent other views in the window from being interacted with, but interaction with them will follow the usual pattern once the finger from the exlusiveTouch-item is removed).

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

Another effect is preventing view A from receiving events as long as some other view is interacted with (keep a button without exclusiveTouch set pressed, and the ones with exclusiveTouch will not be able to receive events as well).

另一个效果是,只要与其他视图交互,视图 A 就无法接收事件(保持未设置exclusiveTouch 的按钮被按下,具有exclusiveTouch 的按钮也将无法接收事件)。

You can still set a button in your view to exclusiveTouch and interact with the others, just not at the same time, as this simple test UIViewController will prove (once the correct bindings in the IB are set for both Outlets and Actions):

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

#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

In light of this, the only good reason IMHO for Apple not to set exclusiveTouch to YES for every UIView subclass is that it would have made the implementation of complex gestures a real PITA, including probably some of the gestures we are already accustomed to in composite UIView subclasses (like UIWebView), as setting selected views to exclusiveTouch=NO (like button) is faster than doing a recursive exclusiveTouch=YES on pretty much everything just to enable multitouch.

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

The drawback of this is that in many cases the counter intuitive behaviour of UIButtons and UITableViewCells (among others...) might introduce weird bugs and make testing more tricky (as it happened to me like... 10 minutes ago? :( ).

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

Hope it helps

希望能帮助到你

回答by Jesse Naugher

the UIViewproperty exclusiveTouchmeans the view (button) is the ONLY thing in that window that can be interacted with if it is set to YES. As stated in the docs: 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.

UIView属性exclusiveTouch意味着视图(按钮)是该窗口中唯一可以与之交互的东西,如果它被设置为YES. 如文档中所述:将此属性设置为 YES 会导致接收器阻止将触摸事件传递到同一窗口中的其他视图。此属性的默认值为 NO。

Therefore, it is the common behavior that you might have multiple buttons or interaction controls/views in a window and want exclusiveTouchset to NO.

因此,您可能在一个窗口中有多个按钮或交互控件/视图并希望exclusiveTouch将其设置为NO.

If you set this property to YESfor any UIView subclass in a window, you can not interact with anything else in the window for as long as that property is set to YES. That means if you initialize a button with exclusiveTouch = YES, but also have a table view or another button or a scroll view or any other view that is based on interaction, it will not respond to any touches.

如果您将此属性设置YES为窗口中的任何 UIView 子类,则只要该属性设置为 ,您就无法与窗口中的任何其他内容交互YES。这意味着如果你用 初始化一个按钮exclusiveTouch = YES,但也有一个表格视图或另一个按钮、滚动视图或任何其他基于交互的视图,它不会响应任何触摸。

回答by hundreth

exclusiveTouch simply means that any view underneath your UIButton will not receive the touch events.

exclusiveTouch 只是意味着 UIButton 下的任何视图都不会收到触摸事件。

It's set to no by default because you typically want the view underneath to receive these events. For example, if you have a UIButton on top of a scroll view and the user wants to scroll. You want the scrollView to scroll even if they begin with their finger on the UIButton.

它默认设置为 no,因为您通常希望下面的视图接收这些事件。例如,如果您在滚动视图顶部有一个 UIButton 并且用户想要滚动。您希望 scrollView 滚动,即使它们以手指在 UIButton 上开始。

回答by Cyprian

I was just reading release notes for iOS 5 and from this version the exclusiveTouch will be set to YES by default. So just keep in mind that it will change with the new version of iOS.

我只是在阅读 iOS 5 的发行说明,从这个版本开始,exclusiveTouch 默认设置为 YES。所以请记住,它会随着 iOS 的新版本而改变。