当用户转到不同的视图控制器时,NSTimer停止

时间:2014-09-23 01:09:38

标签: objective-c xcode uiviewcontroller uibutton nstimer

当用户按下NSTimer时,我会激活UIButton。然后,这将导致运行计时器的方法。当应用程序在后台运行时,计时器会运行,但当再次按下应用程序并且计时器继续运行时,UIView动画会停止。每当我转到另一个UIViewController时,计时器停止并且所有UILabel都恢复其状态,就好像计时器没有运行一样。如何解决此问题,以便当用户使用计时器返回原始视图控制器时计时器仍在运行。

- (IBAction)start:(id)sender {

//WHEN TIMER IS STOPPED!
if([startStop.titleLabel.text isEqualToString:@"Stop"]){

    NSLog(@"Button Stopped");

    stopTime = [NSDate date];

    //NSString Log of Time accquired
    [timer invalidate];
    timerIsRunning = FALSE;

    //Format Stop Time for NSUserDefaults
    NSDateFormatter *saveStopTime = [[NSDateFormatter alloc] init];
    [saveStopTime setDateFormat:@"hh:mm a"];
    [saveStopTime setTimeZone:[NSTimeZone localTimeZone]];

    NSString *saveStopTimeString = [saveStopTime stringFromDate:stopTime];

    //Save Stop Time
    NSUserDefaults *saveStop = [NSUserDefaults standardUserDefaults];
    [saveStop setObject:saveStopTimeString forKey:@"savedStopTime"];
    [saveStop synchronize];

    timeStopLabel.text = [NSString stringWithFormat:@"%@", saveStopTimeString];

    [startStop setTitle:@"Start" forState:UIControlStateNormal];

    startStop.tintColor = [UIColor colorWithRed:0.19 green:0.803 blue:0.19 alpha:1]; // GREEN

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.7];
    [btnSpin setAlpha:0];
    [UIView commitAnimations];
}
else {
//WHEN TIMER IS RUNNING

    NSLog(@"Button Started");

    //Start Date recording when START
    baseDate = [NSDate date];
    startTimeDate = [NSDate date];
    startTime = [NSDate date];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerActivity) userInfo:nil repeats:YES];
    timerIsRunning = TRUE;
    valueInStopLabel = TRUE;

    if (timerIsRunning == FALSE) {
        stopTime = [NSDate date];
    }
    if (valueInStopLabel == TRUE) {
        timeStopLabel.text = @"--:-- --";
    }
    else if (valueInStopLabel == FALSE) {
        timeStartLabel.text = @"--:-- --";
    }

    startStop.tintColor = [UIColor redColor];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [btnSpin setAlpha:1];
    btnSpin.image = [UIImage imageNamed:nil];
    [UIView commitAnimations];

    btnSpin.image = [UIImage imageNamed:@"startSpin.png"];
    spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    spin.byValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    spin.duration = 2;
    spin.repeatCount = MAXFLOAT;
    [btnSpin.layer addAnimation:spin forKey:@"red"];
}
}

- (void)timerActivity {

valueInStopLabel = TRUE;

NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.baseDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

NSString *timeString = [dateFormatter stringFromDate:timerDate];

//Save Date
NSUserDefaults *saveDate = [NSUserDefaults standardUserDefaults];
[saveDate setObject:timeString forKey:@"savedDate"];
[saveDate synchronize];

//Format Start Time Date for NSUserDefaults
NSDateFormatter *saveStartTimeDateFormatter = [[NSDateFormatter alloc] init];
[saveStartTimeDateFormatter setDateFormat:@"d/LL/yy"];
[saveStartTimeDateFormatter setTimeZone:[NSTimeZone localTimeZone]];

NSString *saveTimeDateString = [saveStartTimeDateFormatter stringFromDate:startTimeDate];

//Save Start Time Date
NSUserDefaults *saveTimeDateStart = [NSUserDefaults standardUserDefaults];
[saveTimeDateStart setObject:saveTimeDateString forKey:@"savedStartDateTime"];
[saveTimeDateStart synchronize];

timeStartDateLabel.text = [NSString stringWithFormat:@"%@", saveTimeDateString];

//Format Start Time for NSUserDefaults
NSDateFormatter *saveStartTimeFormatter = [[NSDateFormatter alloc] init];
[saveStartTimeFormatter setDateFormat:@"hh:mm a"];
[saveStartTimeFormatter setTimeZone:[NSTimeZone localTimeZone]];

NSString *saveTimeString = [saveStartTimeFormatter stringFromDate:startTime];

//Save Start Time
NSUserDefaults *saveTimeStart = [NSUserDefaults standardUserDefaults];
[saveTimeStart setObject:saveTimeString forKey:@"savedStartTime"];
[saveTimeStart synchronize];

timeStartLabel.text = [NSString stringWithFormat:@"%@", saveTimeString];

self.timeLabel.text = timeString;

if (timerIsRunning == TRUE) {
        [startStop setTitle:@"Stop" forState:UIControlStateNormal];
    }

if (timerIsRunning == FALSE) {
            [startStop setTitle:@"Start" forState:UIControlStateNormal];
            [timer invalidate];
            timer = nil;
    stopTime = [NSDate date];
}
}

0 个答案:

没有答案