標籤:
****
#import "HMViewController.h"// 每秒秒針轉6度#define perSecendA 6// 每分鐘分針轉6度#define perMinuteA 6// 每小時時針轉6度#define perHourA 30// 每分鐘時針轉6度#define perMinuteHourA 0.5#define angle2radian(x) ((x) / 180.0 * M_PI)@interface HMViewController (){ CALayer *_second; CALayer *_minute; CALayer *_hour;}@property (weak, nonatomic) IBOutlet UIView *redView;@property (weak, nonatomic) IBOutlet UIImageView *clockView;@end@implementation HMViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 1.添加秒針 [self addSecond]; // 2.添加分針 [self addMintue]; // 3.添加時針 [self addHour]; // 建立定時器 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update) userInfo:nil repeats:YES]; [self update];}- (void)addHour{ CGFloat imageW = _clockView.bounds.size.width; CGFloat imageH = _clockView.bounds.size.height; // 1.添加時針 CALayer *hour = [CALayer layer]; // 2.設定錨點 hour.anchorPoint = CGPointMake(0.5, 1); // 3.設定位置 hour.position = CGPointMake(imageW * 0.5, imageH * 0.5); // 4.設定尺寸 hour.bounds = CGRectMake(0, 0, 4, imageH * 0.5 - 50); // 5.紅色 hour.backgroundColor = [UIColor blackColor].CGColor; hour.cornerRadius = 4; // 添加到圖層上 [_clockView.layer addSublayer:hour]; _hour = hour;}// 添加分針- (void)addMintue{ CGFloat imageW = _clockView.bounds.size.width; CGFloat imageH = _clockView.bounds.size.height; // 1.添加分針 CALayer *minute = [CALayer layer]; // 2.設定錨點 minute.anchorPoint = CGPointMake(0.5, 1); // 3.設定位置 minute.position = CGPointMake(imageW * 0.5, imageH * 0.5); // 4.設定尺寸 minute.bounds = CGRectMake(0, 0, 2, imageH * 0.5 - 30); // 5.紅色 minute.backgroundColor = [UIColor blueColor].CGColor; // 添加到圖層上 [_clockView.layer addSublayer:minute]; _minute = minute;}// 添加秒針- (void)addSecond{ CGFloat imageW = _clockView.bounds.size.width; CGFloat imageH = _clockView.bounds.size.height; // 1.添加秒針 CALayer *second = [CALayer layer]; // 2.設定錨點 second.anchorPoint = CGPointMake(0.5, 1); // 3.設定位置 second.position = CGPointMake(imageW * 0.5, imageH * 0.5); // 4.設定尺寸 second.bounds = CGRectMake(0, 0, 1, imageH * 0.5 - 20); // 5.紅色 second.backgroundColor = [UIColor redColor].CGColor; // 添加到圖層上 [_clockView.layer addSublayer:second]; _second = second;}- (void)update{ // 擷取秒數 // 擷取日曆對象 NSCalendar *calendar = [NSCalendar currentCalendar]; // 擷取日期組件 NSDateComponents *compoents = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; // 擷取秒數 CGFloat sec = compoents.second; // 擷取分鐘 CGFloat minute = compoents.minute; // 擷取小時 CGFloat hour = compoents.hour; // 算出秒針旋轉多少° CGFloat secondA = sec * perSecendA; // 算出分針旋轉多少° CGFloat minuteA = minute * perMinuteA; // 算出時針旋轉多少° CGFloat hourA = hour * perHourA; hourA += minute * perMinuteHourA; // 旋轉秒針 _second.transform = CATransform3DMakeRotation(angle2radian(secondA), 0, 0, 1); // 旋轉分針 _minute.transform = CATransform3DMakeRotation(angle2radian(minuteA), 0, 0, 1); // 旋轉時針 _hour.transform = CATransform3DMakeRotation(angle2radian(hourA), 0, 0, 1);}@end
IOS第18天(4,核心動畫,時鐘效果,定時器,圖片旋轉角度,CALayer 錨點,擷取當前,小時,秒,分)