Here’s the basic outline of a class to achieve this. The idea is that the timer fires every second and timer is only accumlated (into _timerValue
) if the paused flag (_paused
) is NO
.
TimerClass.h:
@interface TimerClass : NSObject {
NSTimer *_timer;
BOOL _paused;
NSInteger _timerValue;
}
@end
TimerClass.m:
@implementation TimerClass
- (IBAction)startTimer:(id)sender {
NSAssert(!_timer, @"Already started");
_timerValue = 0;
_paused = NO;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(tick:)
userInfo:nil
repeats:YES];
}
- (IBAction)stopTimer:(id)sender {
[_timer invalidate];
_timer = nil;
}
- (IBAction)pauseTimer:(id)sender {
_paused = YES;
}
- (IBAction)resumeTimer:(id)sender {
_paused = NO;
}
- (void)tick:(NSTimer *)timer {
if (!_paused)
_timerValue++;
}
@end
4
solved Timer: start-pause-resume-stop not working the way it should in ios [closed]