Show time like a digital clock

扶醉桌前 提交于 2019-12-12 09:13:51

问题


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.


回答1:


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;
}



回答2:


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.




回答3:


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);

}


来源:https://stackoverflow.com/questions/11018854/show-time-like-a-digital-clock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!