问题:在iOS应用程序中,只要想象有两个按钮,如下图所示,它们有两个动作,就像一个切换逻辑.
它的逻辑可能如下:
- (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设置为False,我们如何能够检测到isEnabled true或false?
以下是使这个问题成为SO的另一个问题的可能重复的原因:
>即使一些排名较高的代码可能将我的问题标记为可能的重复,Q& A没有给我正确的理解.
正如@danh所说,
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 thatsetUserInteractionEnabled
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.
解决方法
好的,那为什么?
由于UIControl子类继承两者,为什么有两个几乎相同的属性?为什么不控制只是放弃“启用”的想法,并根据userInteractionEnabled状态绘制不同的内容?
至少有一个原因是在动画中,UIViews上的用户交互被禁用.控件在动画时将自己画成灰色是错误的.所以至少在动画时,这两个属性有不同的含义.