自定义时钟 时间

1、

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //定时器 反复执行
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

2、

-(void)updateTime{
    UILabel *timeLable = [[UILabel alloc]initWithFrame:CGRectMake(0,0,200,30)];
    timeLable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:timeLable];
    
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    NSLog(@"现在时间:%@",dateString);
    
    timeLable.text = dateString;
}
原文地址:https://www.cnblogs.com/dujiahong/p/7864948.html