我使用的NSTimer在我的节目播放视频,的NSTimer连续运行在功能 跑我是如何暂停&简历的NSTimer在行动,我的动作是 -暂停和恢复的NSTimer
-(void)handleTap
{
if(pause==YES)
{
//resume ;
}
else
{
//pause;
}
}
我使用的NSTimer在我的节目播放视频,的NSTimer连续运行在功能 跑我是如何暂停&简历的NSTimer在行动,我的动作是 -暂停和恢复的NSTimer
-(void)handleTap
{
if(pause==YES)
{
//resume ;
}
else
{
//pause;
}
}
NSTimer中没有暂停和恢复功能。你可以像下面提到的代码那样做。
- (void)startTimer {
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)fireTimer:(NSTimer *)inTimer {
// Timer is fired.
}
- (void)resumeTimer {
if(m_pTimerObject) {
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)pauseTimer {
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
我希望这段代码片段能帮助你。
感谢您的帮助,对我来说非常有用 – NANNAV
在nstimer中没有具体的方法来暂停和恢复。如果你想停止定时器,你需要使定时器无效。
你不能暂停定时器。
,您可以:
在表演,你可以只使用[timer invalidate];
动作,当你要恢复你刚开始再次你的计时器
请参考下面
How can I programmatically pause an NSTimer?
的链接,我发现这个答案的苹果
您可以存储自定时器以来的时间量开始...当计时器开始将日期存储在NSDate变量中时。 然后,当用户切换...使用 中的方法timeIntervalSinceNow NSDate类来存储已经过了多少时间...请注意,这个 将给timeIntervalSinceNow一个负值。当用户 返回使用该值来设置适当的计时器。
您不能暂停计时器,您可以保存计时器的fireDate以及当前日期。在此之后,您使计时器无效。当需要恢复计时器时,您将创建一个新的计时器对象,并将启动日期设置为旧启动日期以及用户在菜单中的时间(oldTime + currentTime)。
- (IBAction)pauseResumeTimer:(id)sender {
if (timerRunning == NO) {
timerRunning = YES;
[pauseTimerBtn setTitle:@"Resume" forState:UIControlStateNormal];
NSString *stringVal = [NSString stringWithFormat:@"%@",timeTxt.text];
stringVal = [stringVal stringByReplacingOccurrencesOfString:@":" withString:@"."];
float tempFloatVal = [stringVal floatValue];
int minuteValue = floorf(tempFloatVal);
float tempSecVal = [stringVal floatValue] - floorf(tempFloatVal);
int secondVal = tempSecVal*100;
minuteValue = minuteValue*60;
oldTimeValue = minuteValue + secondVal;
[timer invalidate];
timer = nil;
}
else
{
timerRunning = NO;
[pauseTimerBtn setTitle:@"Pause" forState:UIControlStateNormal];
startDate = [NSDate date];
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}
}
- (void)runTimer:(NSTimer *)timer {
NSInteger secondsAtStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
secondsAtStart = secondsAtStart + oldTimeValue;
NSInteger seconds = secondsAtStart % 60;
NSInteger minutes = (secondsAtStart/60) % 60;
NSInteger hours = secondsAtStart/(60 * 60);
NSString *result = nil;
result = [NSString stringWithFormat:@"%02ld:%02ld",(long)minutes,(long)seconds];
timeTxt.text = result;
}
我写了一个控制器类,可以处理暂停和取消暂停计时器。你可以在这里找到它:https://github.com/LianaChu/LCPausableTimer
它的作品非常类似于BornCoder的答案。如果您发现自己需要在项目中经常暂停和取消暂停定时器,则此控制器类可能非常有用。
如果您使用它,我将非常感谢您如何使用它,您希望看到什么更改或您希望我添加的任何功能等任何评论或反馈。请不要犹豫,在这里回复您的意见,或张贴在github页面的问题标签上。
此代码不足以让我们猜测,您用来播放视频的内容! – Dhruv