//// ViewController.m// time////#import "ViewController.h"//获得当前的年月日 时分秒#define CURRENTSEC [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]#define CURRENTMIN [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]#define CURRENTHOUR [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]#define CURRENTDAY [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]#define CURRENTMONTH [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]#define CURRENTYEAR [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]//角度转换成弧度#define ANGEL(x) x/180.0 * M_PI#define kPerSecondA ANGEL(6)#define kPerMinuteA ANGEL(6)#define kPerHourA ANGEL(30)#define kPerHourMinuteA ANGEL(0.5)@interface ViewController ()@property (nonatomic,strong) UIImageView *imageClock;@property (nonatomic, strong) UIView *clockView;@property (nonatomic,strong) CALayer *layerSec;@property (nonatomic,strong) CALayer *layerMin;@property (nonatomic,strong) CALayer *layerHour;@property (nonatomic,strong) CALayer *layerSec2;@property (nonatomic,strong) CALayer *layerMin2;@property (nonatomic,strong) CALayer *layerHour2;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; // Do any additional setup after loading the view, typically from a nib. [self.view addSubview:self.clockView]; [self.clockView.layer addSublayer:self.layerSec]; [self.clockView.layer addSublayer:self.layerMin]; [self.clockView.layer addSublayer:self.layerHour]; [self.view addSubview:self.imageClock]; [self.imageClock.layer addSublayer:self.layerSec2]; [self.imageClock.layer addSublayer:self.layerMin2]; [self.imageClock.layer addSublayer:self.layerHour2]; [self timeChange]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];}- (void)timeChange { self.layerSec.transform = CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1); self.layerMin.transform = CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);// self.layerHour.transform = CATransform3DMakeRotation(CURRENTHOUR * kPerHourA, 0, 0, 1); self.layerHour.transform = CATransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR * kPerHourA, 0, 0, 1); self.layerSec2.transform = CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1); self.layerMin2.transform = CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);// self.layerHour2.transform = CATransform3DMakeRotation(CURRENTHOUR * kPerHourA, 0, 0, 1); self.layerHour2.transform = CATransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR * kPerHourA, 0, 0, 1);}- (UIImageView *)imageClock { if (_imageClock == nil) { _imageClock = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"钟表"]]; _imageClock.frame = CGRectMake(100, 320, 200, 200); _imageClock.layer.cornerRadius = 45; _imageClock.layer.masksToBounds = YES; } return _imageClock;}- (CALayer *)layerSec { if (_layerSec == nil) { _layerSec = [CALayer layer]; _layerSec.bounds = CGRectMake(0, 0, 2, 100); _layerSec.backgroundColor = [UIColor redColor].CGColor; _layerSec.cornerRadius = 5; _layerSec.anchorPoint = CGPointMake(0.5, 0.9);// 固定点 _layerSec.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2); } return _layerSec;}- (CALayer *)layerMin { if (_layerMin == nil) { _layerMin = [CALayer layer]; _layerMin.bounds = CGRectMake(0, 0, 4, 70); _layerMin.backgroundColor = [UIColor blackColor].CGColor; _layerMin.cornerRadius = 5; _layerMin.anchorPoint = CGPointMake(0.5, 0.95); _layerMin.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2); } return _layerMin;}- (CALayer *)layerHour { if (_layerHour == nil) { _layerHour = [CALayer layer]; _layerHour.bounds = CGRectMake(0, 0, 6, 50); _layerHour.backgroundColor = [UIColor blackColor].CGColor; _layerHour.cornerRadius = 5; _layerHour.anchorPoint = CGPointMake(0.5, 0.95); _layerHour.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2); } return _layerHour;}- (CALayer *)layerSec2 { if (_layerSec2 == nil) { _layerSec2 = [CALayer layer]; _layerSec2.bounds = CGRectMake(0, 0, 2, 100); _layerSec2.backgroundColor = [UIColor redColor].CGColor; _layerSec2.cornerRadius = 5; _layerSec2.anchorPoint = CGPointMake(0.5, 0.9);// 固定点 _layerSec2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2); } return _layerSec2;}- (CALayer *)layerMin2 { if (_layerMin2 == nil) { _layerMin2 = [CALayer layer]; _layerMin2.bounds = CGRectMake(0, 0, 4, 70); _layerMin2.backgroundColor = [UIColor blackColor].CGColor; _layerMin2.cornerRadius = 5; _layerMin2.anchorPoint = CGPointMake(0.5, 0.95); _layerMin2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2); } return _layerMin2;}- (CALayer *)layerHour2 { if (_layerHour2 == nil) { _layerHour2 = [CALayer layer]; _layerHour2.bounds = CGRectMake(0, 0, 6, 50); _layerHour2.backgroundColor = [UIColor blackColor].CGColor; _layerHour2.cornerRadius = 5; _layerHour2.anchorPoint = CGPointMake(0.5, 0.95); _layerHour2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2); } return _layerHour2;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (UIView *)clockView { if (_clockView == nil) { _clockView = [[UIView alloc] initWithFrame:CGRectMake(100, 80, 200, 200)]; _clockView.backgroundColor = [UIColor whiteColor]; _clockView.layer.cornerRadius = 100; _clockView.layer.masksToBounds = YES; [self create]; } return _clockView;}- (void)create { for (int i = 1; i <= 60; i++) { CALayer *layer = [CALayer layer]; CGFloat height; if (i % 5 == 0) { int j = (int)(i / 5); height = 10; layer.anchorPoint = CGPointMake(0.5, 10);// 固定点 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 10)]; label.textColor = [UIColor blackColor]; label.text = [NSString stringWithFormat:@"%d", j]; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:12]; label.layer.position = CGPointMake(_clockView.bounds.size.width/2, _clockView.bounds.size.height/2); label.layer.transform = CATransform3DMakeRotation(i * kPerSecondA, 0, 0, 1); label.layer.anchorPoint = CGPointMake(0.5, 8.5); [_clockView addSubview:label]; } else { height = 5; layer.anchorPoint = CGPointMake(0.5, 20);// 固定点 } layer.bounds = CGRectMake(0, 0, 4, height); layer.backgroundColor = [UIColor blackColor].CGColor; layer.position = CGPointMake(_clockView.bounds.size.width/2, _clockView.bounds.size.height/2); layer.transform = CATransform3DMakeRotation(i * kPerSecondA, 0, 0, 1); [_clockView.layer addSublayer:layer]; }}@end