简体   繁体   English

触摸时取消UIButton触摸

[英]Cancel UIButton touch while touching it

I have a situation where a user would be touching a UIButton and while they might already be touching it, I need to be able to cancel it or cause it to fail. 我有一种情况,用户将触摸UIButton,虽然他们可能已经触摸它,我需要能够取消它或导致它失败。

I have tried to use button.enabled and userInteractionEnabled, but neither one works on the fly. 我曾尝试使用button.enabled和userInteractionEnabled,但两者都没有动态。 Both have to be set before the touch begins. 两者都必须在触摸开始之前设置。

Can I cause the button to fail after and while it is being touched? 我可以在触摸后触摸时使按钮失效吗?

You need to register two actions for your button, one for touch down and one for touch up. 您需要为按钮注册两个操作,一个用于触摸,一个用于触摸。 In touch down, you can decide if you want to cancel; 触摸下来,您可以决定是否要取消; and in touch up, write the actual logic for button press. 并在触摸时,写下按钮按下的实际逻辑。

[button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown]; 

- (IBAction)touchDown:(id)sender
{
    UIButton *b = (UIButton*)sender;

    // Call this if you wish to cancel the event tracking
    [b cancelTrackingWithEvent:nil];
}

For reference, see the documentation of -cancelTrackingWithEvent: in UIControl . 有关参考,请参阅UIControl-cancelTrackingWithEvent:的文档

One thing you can do to solve your issue is to customize the pressed appearance of the button to your needs. 您可以做的一件事就是根据需要自定义按钮的按下外观。 Use: – setBackgroundImage:@"NormalStateImage.png" forState: UIControlStateNormal – setBackgroundImage:@"HighlightedStateImage.png" forState: UIControlStateHighlighted 使用: – setBackgroundImage:@"NormalStateImage.png" forState: UIControlStateNormal – setBackgroundImage:@"HighlightedStateImage.png" forState: UIControlStateHighlighted

set the property adjustsImageWhenHighlighted to NO and the adjustsImageWhenDisabled property to NO 将属性adjustsImageWhenHighlighted设置为NO并将adjustsImageWhenDisabled属性设置为NO

This can be done for any UIButton . 这可以针对任何UIButton完成。 If you want to cancel the button press at the time the user touches down on it, these steps should work: 如果要在用户触摸时取消按钮,则这些步骤应该有效:

  1. Set up the handler for the events where the cancellation needs to be checked. 为需要检查取消的事件设置处理程序。 In this case, when the button is down. 在这种情况下,当按钮关闭时。

     [yourButton addTarget: self action: @selector(buttonIsDownHandler:) forControlEvents: UIControlEventTouchDown]; 
  2. In the handler, decide whether to cancel the press 在处理程序中,决定是否取消按下

     -(IBAction) buttonIsDownHandler: (id) sender { if (needToCancelThisPress) { [yourButton cancelTrackingWithEvent: nil]; } } 
  3. The button's handler for UIControlEventTouchCancel will be called after the cancellation. 取消后将调用UIControlEventTouchCancel的按钮处理程序。

The button touch cancellation can be done at any time. 按键触摸取消可以随时进行。 For example, if you want to track when the user presses and holds for too long, you could start a timer in the UIControlEventTouchDown handler, and then cancel the touch when the timer expires. 例如,如果要跟踪用户何时按住并保持太长时间,可以在UIControlEventTouchDown处理程序中启动计时器,然后在计时器到期时取消触摸。

Just have BOOL variable which points either button action is allowed or not and check where necessary. 只需要BOOL变量指向是否允许按钮操作,并在必要时进行检查。 Enabled and userinteractionenabled is just to disable touches in general 启用和userinteractionenabled只是为了禁用一般的触摸

Not sure about your problem but you can have differant actions based on control event like below 不确定您的问题,但您可以根据下面的控制事件进行不同的操作

[button addTarget:self
                     action:@selector(onTouchUpInside:)
           forControlEvents:(UIControlEventTouchUpInside)]; 

[button addTarget:self
                     action:@selector(onTouchDown:)
           forControlEvents:(UIControlEventTouchDown)]; 

[button addTarget:self
                     action:@selector(onTouchUpOutside:)
           forControlEvents:(UIControlEventTouchUpOutside)]; 

Please excuse me if it is not helpful or what you are looking for. 如果没有帮助或者您正在寻找什么,请原谅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM