こんにちは私は小さなgameAppを開発しています。ユーザーが別のビュー[設定ビューなど]に移動したときに、タイマーを一時停止する必要があります。ユーザーがそのビューに戻ったら、タイマーを再開する必要があります。
誰でもこの問題を解決できますか...
前もって感謝します...
こんにちは私は小さなgameAppを開発しています。ユーザーが別のビュー[設定ビューなど]に移動したときに、タイマーを一時停止する必要があります。ユーザーがそのビューに戻ったら、タイマーを再開する必要があります。
誰でもこの問題を解決できますか...
前もって感謝します...
NSTimerには、一時停止する機能はありません。ただし、いくつかの単純な変数を使用して、自分で効果を作成できます。
NSTimer *timer;
double timerInterval = 10.0;
double timerElapsed = 0.0;
NSDate *timerStarted;
-(void) startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:(timerInterval - timerElapsed) target:self selector:@selector(fired) userInfo:nil repeats:NO];
timerStarted = [NSDate date];
}
-(void) fired {
[timer invalidate];
timer = nil;
timerElapsed = 0.0;
[self startTimer];
// react to timer event here
}
-(void) pauseTimer {
[timer invalidate];
timer = nil;
timerElapsed = [[NSDate date] timeIntervalSinceDate:timerStarted];
}
これは私にとって非常にうまくいっています。
このコードを使用して、NSTimerに一時停止および再開機能を実装できます
//=========new timer update method=============
-(void) updateTimer {
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString = [dateFormatter stringFromDate:timerDate];
lblMessage.text = timeString;
pauseTimeInterval = timeInterval;
}
-(IBAction) startBtnPressed:(id)sender
{
//=============================new update with start pause==================
if(running == NO) {
running = YES;
startDate = [NSDate date] ;
startDate = [[startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))] retain];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
else {
running = NO;
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
}
}
.hファイルで宣言NSDatestartDate;
NSTimeInterval pauseTimeinterval; そしてviewdidloadのpausetimeInterval=0.0; 幸運を
タイマーを一時停止することはできません。ただし、ユーザーが設定ビューに移動するfireDate
と、タイマーのと現在の日付を保存できます。この後、タイマーを無効にして、ユーザーに自分の作業を任せます。彼/彼女がゲームに戻ったら、新しいタイマーオブジェクトを作成し、発火日を古い発火日とユーザーがメニューにいた時間(oldTime + currentTime)に設定します。
NSTimerを一時停止することはできません。ただし、無効にして、必要に応じて新しいものを作成することはできます。
on Start -
startNewCapture = [NSDate date];
on Pause -
captureSessionPauseDate = [NSDate date];
captureSessionTimeInterval = [captureSessionPauseDate timeIntervalSinceDate:startNewCapture];
on Resume -
NSDate *dateNow = [NSDate date];
startNewCapture = [NSDate dateWithTimeInterval:-captureSessionTimeInterval sinceDate:dateNow];
- (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;
}
あなたはそれを理解することになったのですか?別のビューに入るときにタイマーを無効にすることはできないとあなたが言ったのを見ました。それが何を意味するのか説明できますか?NSTimersは一時停止できません。一時停止をシミュレートする方法では、通常、NSTimersを無効にする必要があります。次に、新しいタイマーを作成して「一時停止解除」をシミュレートし、タイマーを再起動します。これは、一時停止および一時停止されていないタイマーをシミュレートします。
潜在的により便利なメソッドが必要な人のために、タイマーの一時停止と一時停止解除を便利に処理できるコントローラークラスを作成しました。ここで見つけることができます:https ://github.com/LianaChu/LCPausableTimer
私が作成したコントローラークラスを使用して新しいタイマーを作成し、コントローラークラスのメソッド"pauseTimer"および"unpauseTimer"を呼び出すことで、タイマーを一時停止および一時停止解除できます。
使用方法、見たい変更、追加したい機能など、コメントやフィードバックをいただければ幸いです。ここにコメントを返信するか、Githubページの[問題]タブに投稿してください。