我正在尝试在媒体播放器中设置一个“FastForward/Next”按钮,可以点击该按钮移动到下一首歌曲或按住该按钮以在当前歌曲中快进。大多数情况下,它是有效的:你可以成功地快进并成功地移动到下一首歌曲,但问题是,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 的回答,还是发生了其他事情?