我試圖在媒體播放器中設置一個「FastForward/Next」按鈕,可以輕觸移動到下一首歌曲或在當前歌曲中保持快進。大多數情況下,它的工作原理是:你可以成功地快速前進,併成功移動到下一首歌曲,但事實是,使它工作的NSTimer
從不會失效,所以一旦開始快速前進,就永遠不會停止。UIGestureRecognizer與NSTimer不會死亡
我設置的手勢識別在viewDidLoad
:
UITapGestureRecognizer *singleTapFastForward = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(nextSong:)];
singleTapFastForward.numberOfTapsRequired = 1;
[_buttonNext addGestureRecognizer:singleTapFastForward];
_holdFastForward = [[UILongPressGestureRecognizer alloc] initWithTarget: self action:@selector(startFastForward:)];
[_buttonNext addGestureRecognizer:_holdFastForward];
[singleTapFastForward requireGestureRecognizerToFail:_holdFastForward];
,這裏是函數的肉:
- (IBAction)startFastForward:(id)sender {
_timerFastForward = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(executeFastForward) userInfo:nil repeats:YES];
}
- (void)executeFastForward {
[_avPlayer seekToTime:CMTimeMake(CMTimeGetSeconds([_avPlayer currentTime]) + 10, 1)];
if(_holdFastForward.state == 0) {
[self endFastForward:self];
}
}
- (IBAction)endFastForward:(id)sender {
[_timerFastForward invalidate];
}
這裏是棘手的部分:當我在if(_holdFastForward.state == 0)
行設置斷點,只要我放開按鈕(它應該)就開始工作,併成功調用endFastForward
方法。據我的估計,這應該殺死計時器並結束整個週期,但隨後再次調用executeFastForward
,然後再一次。 invalidate
行似乎什麼都不做(並且我的代碼中沒有其他要點叫executeFastForward
)。
任何想法?這看起來很簡單,如果invalidate
一行工作,一切都將是完美的。我只是不知道爲什麼executeFastForward
繼續被稱爲。是我的NSTimer
TRON對Highlander的答案,還是還有其他事情正在進行?
沒有幫助。 :/但我確實得到了一個線索:在連接remoteControlReceivedWithEvent(使用相同的NSTimer方法完美工作)之後,我可以放心地說,它與啓動整個事件的'UIGestureRecognizer'有關。任何想法爲什麼會搞亂'invalidate'方法? – Nerrolken