xcode 像数字时钟一样显示时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11018854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:36:00  来源:igfitidea点击:

Show time like a digital clock

objective-cxcodeipadnsdatensdateformatter

提问by Breakpoint

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

我可以使用代码在我的 iPad 应用程序上显示当前时间,

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.

但这仅在加载应用程序时给出时间。我怎样才能有时间继续跑步?就像一个数字时钟。

回答by Paresh Navadiya

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

回答by trumpetlicks

Implement an NSTimer

实现一个 NSTimer

How do I use NSTimer?

我如何使用 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!!!

然后实现 targetMethod 来检查和更新你的时间!!!

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

如果是我,我可能会得到初始时间,并且只使用 1 秒计时器更新我的内部时间。

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.

您可以通过实现更快的计时器(比方说快 4 到 8 倍)来获得更高的计时分辨率,这样您可能不会经常不同步,但是如果您这样做了,那么您将能够重新同步到[NSData date] 返回的时间。换句话说,您的后台任务运行得越快,您就越容易与返回的真实时间重新同步。这也意味着您只会在目标方法中每隔几次检查一次同步。

Guess what Im saying is to remember Nyquist. Nyquist's theory (essentially) states that you should sample at leasttwice 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.

猜猜我说的是记住奈奎斯特。Nyquist 的理论(本质上)指出,您的采样速度应该至少是您最终尝试使用从采样中获得的数据集重现的分辨率的两倍。在这种情况下,如果您试图每秒向用户显示一次更新,那么您确实应该以不慢于 1/2 秒的速度进行采样,以尝试捕捉从一秒状态到下一秒状态的转换。

回答by Anand Nanavaty

Note:- Declare in .h file

注意:- 在 .h 文件中声明

@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

注意:- 在 .m 文件中声明

- (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

//这里赋值(tick)方法

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

}