Show time like a digital clock

2019-04-13 21:05发布

I am able to display the current time on my iPad application using the code,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
[dateFormatter setTimeStyle: NSDateFormatterShortStyle];


NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]];
timeLabel.text = currentTime;

But this gives the time only when the application is loaded. How do i get the time to keep running? Like a digital clock.

3条回答
姐就是有狂的资本
2楼-- · 2019-04-13 21:24

Implement an NSTimer

How do I use NSTimer?

[NSTimer scheduledTimerWithTimeInterval:1.0     
                                 target:self
                               selector:@selector(targetMethod:)     
                               userInfo:nil     
                                repeats:YES];

Then implement the targetMethod to check for and update your time!!!

If it were me, I would probably get the initial time, and only update my internal time using a 1 second timer.

You can go for higher timing resolution by implementing a faster timer (lets say 4 to 8 times faster), with this you will probably not get out of sync very often, but if you did, then you would be able to re-synchronize to the time returned by [NSData date]. In other words the faster your background task runs, the easier it would be for you to re-sync with the true time returned. This would also mean that you would only check for syncronization once out of every couple of times inside your target method.

Guess what Im saying is to remember Nyquist. Nyquist's theory (essentially) states that you should sample at least twice as fast as the resolution that you will ultimately try to reproduce using the dataset obtained from your sampling. In this case if you are trying to display to the user a once per second update, then you really should be sampling at no slower then 1/2 of a second to try and catch the transition from one second state to the next.

查看更多
可以哭但决不认输i
3楼-- · 2019-04-13 21:34

Note :- Declare in .h file

@property(nonatomic , weak) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIImageView *imgViewClock; //Image of Wall Clock
@property (weak, nonatomic) IBOutlet UIImageView *hourHandImgView; //Image of Hour hand
@property (weak, nonatomic) IBOutlet UIImageView *minuteHandImgView; //Image of Minute hand
@property (weak, nonatomic) IBOutlet UIImageView *secondHandImgView; //Image of Second hand

Note :- Declare in .m file

- (void)viewDidLoad {
[super viewDidLoad];
//Clock
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
[self tick];
}

//Here assign (tick) Method

-(void)tick {

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *Components = [calendar components:units fromDate:[NSDate date]];
CGFloat hours = (Components.hour / 12.0) * M_PI * 2.0;
CGFloat mins = (Components.minute / 60.0) * M_PI * 2.0;
CGFloat seconds = (Components.second / 60.0) * M_PI * 2.0;

self.hourHandImgView.transform = CGAffineTransformMakeRotation(hours);
self.minuteHandImgView.transform = CGAffineTransformMakeRotation(mins);
self.secondHandImgView.transform = CGAffineTransformMakeRotation(seconds);

}
查看更多
手持菜刀,她持情操
4楼-- · 2019-04-13 21:47

Use this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
[dateFormatter setTimeStyle: NSDateFormatterShortStyle];

[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:YES]

Selector method would be like this:

-(void)targetMethod:(id)sender
{
  NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]];
  timeLabel.text = currentTime;
}
查看更多
登录 后发表回答