本文实例为大家分享了iOS实现简易钟表的具体代码,供大家参考,具体内容如下
效果图:
注意:表盘是一个UIImageView控件,设置image为表盘图片
核心代码:
// // ViewController.m // 时钟 // // Created by llkj on 2017/8/29. // Copyright © 2017年 LayneCheung. All rights reserved. // #import "ViewController.h" //每一秒旋转多少度 #define perSecA 6 //每一分旋转多少度 #define perMinA 6 //每一小时旋转多少度 #define perHourA 30 //每一分时针旋转的度数 #define perMinHour 0.5 //角度转弧度 #define angle2Rad(angle) ((angle) / 180.0 * M_PI) @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *clockView; @property (nonatomic, weak) CALayer *secL; @property (nonatomic, weak) CALayer *minL; @property (nonatomic, weak) CALayer *hourL; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setHour]; [self setMin]; [self setSec]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; [self timeChange]; } - (void)timeChange{ //获取当前秒 NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *cmp = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; NSInteger curSec = cmp.second + 1; NSInteger curMin = cmp.minute; NSInteger curHour = cmp.hour; //秒针开始旋转 //计算秒针当前旋转的角度 // angle = 当前多少秒 * 每一秒旋转多少度 CGFloat secA = curSec * perSecA; //旋转方向是Z轴 self.secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1); //分针开始旋转 //计算分针当前旋转的角度 // angle = 当前多少分 * 每一分旋转多少度 CGFloat minA = curMin * perMinA; self.minL.transform = CATransform3DMakeRotation(angle2Rad(minA), 0, 0, 1); //时针开始旋转 //计算时针当前旋转的角度 // angle = 当前多少时 * 每一小时旋转多少度 CGFloat hourA = curHour * perHourA + curMin * perMinHour; self.hourL.transform = CATransform3DMakeRotation(angle2Rad(hourA), 0, 0, 1); } //添加秒针 - (void)setSec{ CALayer *secL = [CALayer layer]; secL.bounds = CGRectMake(0, 0, 1, 80); secL.backgroundColor = [UIColor redColor].CGColor; //绕着锚点旋转 secL.anchorPoint = CGPointMake(0.5, 1); secL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5); [self.clockView.layer addSublayer:secL]; self.secL = secL; } //添加分针 - (void)setMin{ CALayer *minL = [CALayer layer]; minL.bounds = CGRectMake(0, 0, 3, 70); minL.cornerRadius = 1.5; minL.backgroundColor = [UIColor blackColor].CGColor; minL.anchorPoint = CGPointMake(0.5, 1); minL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5); [self.clockView.layer addSublayer:minL]; self.minL = minL; } //添加时针 - (void)setHour{ CALayer *hourL = [CALayer layer]; hourL.bounds = CGRectMake(0, 0, 3, 60); hourL.backgroundColor = [UIColor blackColor].CGColor; hourL.anchorPoint = CGPointMake(0.5, 1); hourL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5); [self.clockView.layer addSublayer:hourL]; self.hourL = hourL; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。