gpt4 book ai didi

iOS UIButton - UIButton setUserInteractionEnabled 和 setEnabled 之间的区别

转载 作者:技术小花猫 更新时间:2023-10-29 11:00:40 25 4
gpt4 key购买 nike

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

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

enter image description here

其逻辑可能如下:

- (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 方法有什么大的区别。它们的行为就像一个阻止用户不让使用它的单一方法。但是,如果它是相同的,即使 setUserInteractionEnabled 是,我们如何能够检测到 isEnabled truefalse设置为 False?

以下是使这个问题不可能与 SO 中的另一个问答线程重复的原因:

  • 即使某些排名靠前的代码可能已将我的问题标记为可能重复,但该问答并没有给我正确的理解。
  • 正如@danh 所说,

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

给了我真正的答案或者说原因,看到这两种方法有两个原因。因为任何人都可以说 setUserInteractionEnabled 不会对 UI 状态进行更改,但至少只有 @danh 的回答首先声明它可能会在 UI 动画期间隐式使用。

最佳答案

它们几乎相同。 userInteractionEnabledUIView 的一个属性,用于切换 View 是否接收任何用户触摸。 enabledUIControl 的属性(它是 UIView 的子类和 UIButton 的父类(super class))并且具有同样的效果。一个区别是 UIKit 控件可能会根据它们的 enabled 状态以不同方式绘制自己,而抽象 UIView 则不是这种情况。

好吧,那为什么呢?

既然UIControl的子类都继承了两者,为什么会有两个几乎一样的属性呢?为什么控件不放弃“已启用”的想法,并根据它们的 userInteractionEnabled 状态以不同的方式绘制自己?

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

关于iOS UIButton - UIButton setUserInteractionEnabled 和 setEnabled 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34081153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com