對不起,這是一個重複的問題,但我嘗試了所有我找不到的解決方案。NSTimer未能停止?
無論如何,這是我的問題,具體而言。我有以下代碼,在長按手勢時啓動計時器,然後在取消手勢或動作完成時停止計時。
-(void)photoLongPress:(UILongPressGestureRecognizer *)press
{
NSTimer *timer = nil;
if (press.state == UIGestureRecognizerStateBegan)
{
//Start timer
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(photoLongPressTimer:) userInfo:nil repeats:YES];
}
if (press.state == UIGestureRecognizerStateEnded)
{
[timer invalidate];
timer = nil;
}
}
-(void)photoLongPressTimer:(NSTimer *)timer
{
DAProgressOverlayView *progress = (DAProgressOverlayView *)[self.view viewWithTag:kTagPhotoDeleteProgressIndicator];
progress.progress += 0.08;
if (progress.progress == 1)
{
[self deletePhotoSelection];
[timer invalidate];
timer = nil;
}
}
定時器在photoLongPressTimer
方法無效時,但如果所述手勢在photoLongPress
方法停止停止。爲什麼會這樣呢?我很難過。
謝謝。
使用代碼這個偉大的工程。我很好奇,爲什麼當一個局部變量(第一次出現的葉子)似乎是對原始對象的引用時不工作? –
您在最初創建計時器時有參考,但每次手勢改變時,再次調用該方法時會創建一個新的本地參考(並設置爲零)。 – Wain