iOS UIButton - Difference between UIButton setUserInteractionEnabled and setEnabled

前端 未结 3 1592
一个人的身影
一个人的身影 2021-02-13 19:44

Wait!!!:
I know that you may think this question have been asked and answered several time before. But I can guarantee you that this questi

相关标签:
3条回答
  • 2021-02-13 20:11

    Characteristics of enabled:

    • It's a property of UIControl
    • Superclass for UIButton.
    • It has effects on the visual state of the object and is generally the preferred method of disabling a control

    Characteristics of userInteractionEnabled:

    • A property of UIView
    • Code that interacts with your controls is more likely to check if buttons are enabled than if their userInteractionEnabled property is set. It's more conventional.
    0 讨论(0)
  • 2021-02-13 20:12
    @property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
    

    A Boolean value that determines whether user events are ignored and removed from the event queue. When set to NO, user events—such as touch and keyboard—intended for the view are ignored and removed from the event queue. When set to YES, events are delivered to the view normally. The default value of this property is YES.

    Discussion:

    During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. You can disable this behavior by specifying the UIViewAnimationOptionAllowUserInteraction option when configuring the animation.

    Apple Doc on UIView

    @property(nonatomic, getter=isEnabled) BOOL enabled
    

    A Boolean value that determines whether the receiver is enabled.

    Discussion:

    Specify YES to make the control enabled; otherwise, specify NO to make it disabled. The default value is YES. If the enabled state is NO, the control ignores touch events and subclasses may draw differently.

    For your reference:

    1. Apple Doc on UIControl
    2. SO Q&A

    As @danh states:

    "At least one reason is that during animation, user interaction is disabled on UIViews. It would be wrong for controls to draw themselves as greyed out while they are animated. So at least during animation, the two properties have distinct meanings."

    0 讨论(0)
  • 2021-02-13 20:34

    They are nearly the same. userInteractionEnabled is a property of UIView that toggles whether the view receives any user touches. enabled is a property of UIControl (which is a subclass of UIView and a superclass of UIButton) and has the same effect. One difference is that UIKit controls may draw themselves differently depending on their enabled state, which isn't the case for the abstract UIView.

    Okay, then why?

    Since UIControl subclasses inherit both, why are there two almost-the-same properties? Why don't controls just drop the idea of "enabled" and draw themselves differently based on their userInteractionEnabled state?

    At least one reason is that during animation, user interaction is disabled on UIViews. It would be wrong for controls to draw themselves as greyed out while they are animated. So at least during animation, the two properties have distinct meanings.

    0 讨论(0)
提交回复
热议问题