iOS向上触摸禁用按钮

问题描述:

如果UIButton已被禁用[myButton setEnabled:NO];是否可以使用仍然有效的触摸侦听器?iOS向上触摸禁用按钮

我曾尝试:

[self addTarget:self action:@selector(myButtonTUI:) forControlEvents:UIControlStateDisabled]; 

,但无济于事。

回答你的问题是没有

enabled 
A Boolean value that determines whether the receiver is enabled. 

@property(nonatomic, getter=isEnabled) BOOL 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. 

可以模拟这样的功能?是。

而不是禁用该按钮,请检查功能是否应在按钮动作选择器上触发。你甚至可以改变图像,使其看起来被禁用。这样按钮仍然会收到触摸事件,并且如果符合正确的条件,您可以触发所需的功能。

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myButtonHit)]; 
[self.myButtonView addGestureRecognizer:gesture]; 

出于某种原因,它似乎没有附接时的工作:

+0

有一个比这更好的解决方案。我们不能只将另一个手势识别器附加到按钮上吗? – devios1 2013-09-03 21:43:09

+0

为了记录,我通过将按钮放入“UIView”并将一个手势识别器附加到“UIView”来实现此目的。由于某些原因,将它直接连接到按钮时不起作用。 – devios1 2013-09-03 22:53:00

+0

您将遇到无障碍方面的问题。 – rshev 2016-11-24 11:40:55

这可以通过在相同尺寸的UIView包裹UIButton并附加敲击手势识别器到父视图来实现手势识别器到UIButton本身,这本来是最好的解决方案。