iOS实现简易钟表
程序员文章站
2023-11-22 14:08:40
本文实例为大家分享了ios实现简易钟表的具体代码,供大家参考,具体内容如下效果图:注意:表盘是一个uiimageview控件,设置image为表盘图片核心代码://// viewcontroller....
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: orcl regexp_like 的用法
下一篇: PowerDesigner逆向工程