iOS UIButton - UIButton setUserInteractionEnabled和setEnabled之间的区别 [英] iOS UIButton - Difference between UIButton setUserInteractionEnabled and setEnabled

查看:351
本文介绍了iOS UIButton - UIButton setUserInteractionEnabled和setEnabled之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等等!!!:
我知道你可能会认为这个问题已经被问过几次了。但我可以保证你这个问题是独一无二的。

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

问题:
在iOS应用中,想象一下有两个按钮,如下图所示,它们有两个动作就像一个切换逻辑。

Question:
In an iOS App, just imagine two buttons are there, like shown in the image bellow, and they have two actions which behaves like a toggling logic.

它的逻辑可能如下:

- (IBAction)testBtnClicked:(id)sender {
    if ([self.testBtn isEnabled]) {
        [self.testBtn setEnabled:NO];
        [self.setInteractionBtn setUserInteractionEnabled:YES];
    } else {
        [self.testBtn setEnabled:YES];
        [self.setInteractionBtn setUserInteractionEnabled:NO];
    }
}

- (IBAction)setInteractionBtnClicked:(id)sender {
    if ([self.setInteractionBtn isEnabled]) {
        [self.setInteractionBtn setUserInteractionEnabled:NO];
        [self.testBtn setEnabled:YES];
    } else {
        [self.setInteractionBtn setUserInteractionEnabled:YES];
        [self.testBtn setEnabled:NO];
    }
}

所以我看不出 setEnabled 方法和 setUserInteractionEnabled 方法。它们的行为类似于阻止用户不使用它的单一方法。但是,如果它是相同的,我们怎么能够检测到 isEnabled true false 即使 setUserInteractionEnabled 设置为 False

So I don't see a big difference between setEnabled method and setUserInteractionEnabled method. They same behave like a single method which blocks the user not letting use it. However if it is same alike, How could we be able to detect isEnabled true or false even when setUserInteractionEnabled is set to be False?

以下是使问题无法与SO中另一个Q& A线程重复的原因:


  • 尽管一些排名较高的代码可能会将我的问题标记为可能重复,但问答并没有给我正确的理解。

  • As @danh说,

  • Even though some high ranked code may have marked my question as a possible duplicate, that Q&A didn't give me the correct understanding.
  • As @danh said,

至少有一个原因是在动画期间,UIViews上的用户交互是
禁用。控件在动画时将自己画成
为灰色是错误的。所以至少在动画期间,
这两个属性有不同的含义。
给我真正的答案或原因,看出这两种方法有两个原因。因为任何人都可以说 setUserInteractionEnabled 不会对UI状态做任何更改,但至少只有@ danh的回答首先声明它可能会在UI动画中隐式使用。

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. Gave me the real answer or the reason to see that these two methods are for two reasons. Because anyone could say that setUserInteractionEnabled doesn't do changes on UI state, but at least only on @danh's answer had first stated that it might be implicitly used during UI Animations.


推荐答案

它们几乎相同。 userInteractionEnabled UIView 的属性,用于切换视图是否接收到任何用户触摸。 已启用 UIControl 的属性(这是 UIView 和超类 UIButton )并具有相同的效果。一个区别是UIKit控件可能会根据其启用的状态自行绘制,这与抽象 UIView

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.

好的,那么为什么?

UIControl 子类继承两者,为什么有两个几乎相同的属性?为什么不控制只是放弃启用的想法,并根据 userInteractionEnabled 状态以不同方式绘制自己?

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?

至少有一个原因是在动画期间, UIView 上禁用了用户交互。控件在动画时将自己画成灰色是错误的。所以至少在动画期间,这两个属性具有不同的含义。

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.

这篇关于iOS UIButton - UIButton setUserInteractionEnabled和setEnabled之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆