Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 像数字钟一样显示时间_Objective C_Xcode_Ipad_Nsdate_Nsdateformatter - Fatal编程技术网

Objective c 像数字钟一样显示时间

Objective c 像数字钟一样显示时间,objective-c,xcode,ipad,nsdate,nsdateformatter,Objective C,Xcode,Ipad,Nsdate,Nsdateformatter,我可以使用代码在我的iPad应用程序上显示当前时间 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle: NSDateFormatterShortStyle]; NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]]; timeLabel.text = currentTim

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

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


NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]];
timeLabel.text = currentTime;
但这仅在加载应用程序时提供时间。我怎样才能有时间继续跑步?就像一个数字时钟。

实现一个NSTimer

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

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

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

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

使用以下方法:

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

[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:YES]
选择器方法如下所示:

-(void)targetMethod:(id)sender
{
  NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]];
  timeLabel.text = currentTime;
}

注意:-在.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

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

- (void)viewDidLoad {
[super viewDidLoad];
//Clock
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
[self tick];
}
//此处指定(勾选)方法


不要保留计数器,只要在计时器滴答作响时显示当前时间即可。@Josh Caswell-在查找NSDate后同意,那就好了。这些年来,我一直在处理硬件问题,并在nats上落后,以便在准确的瞬间完成任务,这就是奈奎斯特作品的用武之地:-)
- (void)viewDidLoad {
[super viewDidLoad];
//Clock
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
[self 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);

}