#import "ActivityViewController.h"
@interface ActivityViewController ()
@property (weak, nonatomic) IBOutlet UIView *clockView;
@property (weak , nonatomic) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *hoursLabel;
@property (weak, nonatomic) IBOutlet UILabel *minutesLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondsLabel;
@property (weak, nonatomic) IBOutlet UILabel *miliLabel;
@end
@implementation ActivityViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
NSDate *start;
- (IBAction)pause:(UIButton *)sender {
[self.timer invalidate];
}
-(void)startTimer {
start = [[NSDate alloc] init];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(count) userInfo:nil repeats:true];
}
- (IBAction)startCounting:(UIButton *)sender {
[self startTimer];
}
-(void)count {
NSDate *now = [[NSDate alloc] init];
NSTimeInterval interval = [now timeIntervalSinceDate:start];
self.hoursLabel.text = [NSString stringWithFormat:@"%@", [self hourString:interval]];
self.minutesLabel.text = [NSString stringWithFormat:@"%@", [self minuteString:interval]];
self.secondsLabel.text = [NSString stringWithFormat:@"%@", [self secondString:interval]];
self.miliLabel.text = [NSString stringWithFormat:@"%@", [self miliString:interval]];
}
-(NSString *)hourString:(NSTimeInterval)timeInterval {
NSInteger interval = timeInterval;
long hours = (interval / 3600);
return [NSString stringWithFormat:@"%0.2ld", hours];
}
-(NSString *)minuteString:(NSTimeInterval)timeInterval {
NSInteger interval = timeInterval;
long minutes = (interval / 60) % 60;
return [NSString stringWithFormat:@"%0.2ld", minutes];
}
-(NSString *)secondString:(NSTimeInterval)timeInterval {
NSInteger interval = timeInterval;
long seconds = interval % 60;
return [NSString stringWithFormat:@"%0.2ld", seconds];
}
-(NSString *)miliString:(NSTimeInterval)timeInterval {
NSInteger ms = (fmod(timeInterval, 1) * 100);
return [NSString stringWithFormat:@"%0.2ld", ms];
}
@end
This my whole implementation of the view controller
I try to make a stopwatch, but when I try to make this in objective-C updating UI in miliseconds slows down after 3 seconds and frozes simulator and computer after 6 seconds. Is there any solution? In swift it works very smoothly but when it comes to obj-c I have problem.