这篇文章主要为大家详细介绍了iOS实现简易钟表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了iOS实现简易钟表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了iOS实现简易钟表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了ios实现简易钟表的具体代码,供大家参考,具体内容如下
效果图:

注意:表盘是一个uiimageview控件,设置image为表盘图片
核心代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
//// 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 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持米米素材网。
原文链接:https://blog.csdn.net/u010981736/article/details/77679185
发表评论