UIButton,如何防止双重动作发生 [英] UIButton, how to prevent double actions taking place

查看:24
本文介绍了UIButton,如何防止双重动作发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定 UIButton 是如何工作的.例如,如果我有一个计时器在我按下开始按钮时会关闭,我该如何制作,以便再次按下按钮时,它不会启动另一个计时器.因为那么我的计时器会快两倍.如果按钮被按下两次,我似乎想让我原来的计时器失效并启动一个新的计时器,但我不知道你怎么知道按钮是否被按下了两次.

I'm not quite sure how UIButton works. For example, if I have a timer that goes off when I hit the start button, how do I make it so that if the button gets pressed again, it doesn't start another timer. Cause then my timer is going twice as fast. It seems like I'd want to invalidate my original timer if the button gets pressed twice and start a new timer, but I don't know how you can tell if the button gets pressed twice.

另外,有没有办法在按下一次 UIButton 后更改标签,然后在再次按下时将其恢复?比如,播放/暂停?谢谢.

Also, is there a way to change the label on the UIButton once its pressed once, and then have it revert back when it gets pressed again? Like, Play/Pause? Thanks.

推荐答案

封装和状态管理对于这种情况非常有用.根据您希望封装的程度,您可能会考虑让 UIButton 修改一个新对象(您提到了播放/暂停,所以我在想象某种媒体播放器?)

Encapsulation and state management are very useful for situations like this. Depending on how much you wish to encapsulate, you might consider having a new object to be modified by the UIButton (you mention Play/Pause so I'm imagining some kind of media player?)

(您可能会考虑在本示例中使用 enumeration 状态以使其更具可扩展性,但为简单起见,我将使用 BOOL)

(You might consider an enumeration of states in this example to make it more extendable, but for simplicity I'll use a BOOL)

@interface MediaManager : NSObject
{

    BOOL isPlaying;  // whether
    NSTimer *playTimer; // timer used exclusively by the media manager
    //... other vars relating to type of media/selected track etc   anything related
}

-(void) togglePlay;


@property (nonatomic, synthesize) NSTimer *playTimer;
@propety (nonatomic, assign) BOOL isPlaying;

@end


@implementation MediaManager

@synthesize playTier, isPlaying;


- (void)togglePlay 
{
    if (!self.isPlaying)  
    {
        if (self.playTimer == nil) // if it hasn't already been assigned
        {
            self.playTimer = [NSTimer ...]; // create and schedule the timer
            self.isPlaying = YES;
        }
    }
    else 
    {
        if (self.playTimer != nil)
        {
            // get the timer and invalidate it
            self.isPlaying = NO;
        }
    }

}

@end

虽然这不是最好的例子,但是通过将状态封装在一个单独的对象中,您的视图 UIButton 可以完全依赖模型状态来呈现自己(假设它持有对初始化媒体管理器:

This is not the best though out example, but by encapsulating the state within a seperate object, your view UIButton can rely solely on the model state for presenting itself (assuming it holds a reference to an initialized Media Manager:

-(IBAction) buttonPressed: (id) sender
{
    [self.mediaManager togglePlay]; // play from whatever state it is in 

    // update the button text to reflect state
    if (self.mediaManager isPlaying)
    {
        self.mediaButton.textLabel.text = @"Pause";
    }
    else
    {
        self.mediaButton.textLabel.text = @"Play";
    }

}

这样您就不必在每次想要使用此功能时都包含状态处理,并且按钮始终反映它所作用的对象的真实状态.

This way you don't have to include the state handling each time you want to use this functionality, and the button always reflects the true state of the object it is acting on.

您可以更进一步,将状态字符串也封装在对象中,可以用类似的方式查询.

You might go a step further and encapsulate the state strings in the object also, which could be queried in a similar manner.

(为写得不好的代码道歉,我还没有喝过咖啡)

(Apologies for poorly written code, I've not had a coffee yet)

这篇关于UIButton,如何防止双重动作发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆