双击按钮时,NSTimer不会停止

时间:2012-06-27 06:55:54

标签: objective-c ios5 xcode4.2 nstimer

当我点击按钮时,我有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; 

}

1 个答案:

答案 0 :(得分:0)

使用双击它会安排计时器两次。 如果需要,可以创建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;
}