iOS在禁用按钮内部触摸

时间:2012-03-05 20:51:17

标签: ios uibutton

如果UIButton已禁用[myButton setEnabled:NO];,是否可以使用仍然有效的触控侦听器?

我试过了:

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

但无济于事。

2 个答案:

答案 0 :(得分:4)

您的问题的答案是否

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.

你能模拟这样的功能吗?是。

不要禁用该按钮,而是检查是否应该在按钮操作选择器上触发该功能。您甚至可以更改图像以使其看起来禁用。这样,按钮仍然会接收触摸事件,如果满足正确的条件,您可以触发所需的功能。

答案 1 :(得分:2)

这可以通过将UIButton包裹在相同大小的UIView中并将点击手势识别器附加到父视图来实现:

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

由于某种原因,将手势识别器附加到UIButton本身似乎不起作用,这本来是最好的解决方案。

相关问题