-1

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; 

}
4

1 に答える 1

0

ダブルクリックすると、タイマーが 2 回スケジュールされます。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;
}
于 2012-06-27T07:08:35.827 に答える