【问题标题】:NSTimer is Not stopping when double Click on a Button双击按钮时NSTimer不会停止
【发布时间】:2012-06-27 06:55:53
【问题描述】:

当我点击按钮时,我有NSTimer 在 2 秒后显示图像。当我单击按钮时它工作正常,但问题是当我双击按钮时,NSTimer 没有停止。它连续显示图像(调用NSTimer 方法),而无需单击下次按钮。当我一次双击/多次点击该按钮时如何停止NSTimer

*This is my Code*

-(void)buttonClicked
{

timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerClicked) userInfo:nil repeats:YES];
imgviewtransparent.image = [UIImage imageNamed:[imagesArray objectAtIndex:i]];
}


-(void)timerClicked
{
    [timer invalidate];
    timer = nil; 

}

【问题讨论】:

  • 为什么你认为双击按钮会停止?唯一会发生的是,您的回调将在您单击按钮时被多次调用......并且回调本身不会停止/使计时器无效;它只是安排另一个。但是,您能否附上更多代码或链接按钮事件的方式?祝你好运!

标签: objective-c ios5 xcode4.2 nstimer


【解决方案1】:

双击它会安排计时器两次。 如果需要,您可以创建一个 BOOL 属性来避免这种情况。

-(void)viewDidLoad{
    [super viewDidLoad];
    //Define all your implementation

    //Set BOOL property to no at start
    self.isButtonClicked = NO; // You need to define this BOOL property as well.
}

-(void)buttonClicked {
    // Return if one click is already in process
    if (self.isButtonClicked) {
        return;
    }

    self.isButtonClicked = YES:
    timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self         
    selector:@selector(timerClicked) userInfo:nil repeats:YES];
    imgviewtransparent.image = [UIImage imageNamed:[imagesArray objectAtIndex:i]];
}


-(void)timerClicked {
    [timer invalidate];
    timer = nil; 
    self.isButtonClicked = NO;
}

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 2023-03-17
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多