特殊控件 - 钟表效果实现
变更记录
序号 | 录入时间 | 录入人 | 备注 |
---|---|---|---|
1 | 2015-03-26 | Alfred Jiang | - |
2 | 2015-12-23 | Alfred Jiang | - |
方案名称
特殊控件 - 钟表效果实现
关键字
特殊控件 \ 时间 \ 表盘 \ 钟面 \ 时钟
需求场景
- 需要时钟效果的页面
参考链接
详细内容
#####1. Swift 版本 EZClockView
静态表盘,需要使用类似倒计时器来不断刷新赋值
1 | // Copyright (c) 2015 Benoit Layer |
#####2. Objective-C 版本 Clock View
动态表盘,较早版本,MRC编写,需要设置 -fno-objc-arc
ClockView.h
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//
//
// clock
//
// Created by Ignacio Enriquez Gutierrez on 1/31/11.
// Copyright 2011 Nacho4D. All rights reserved.
// See the file License.txt for copying permission.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ClockView : UIView {
CALayer *containerLayer;
CALayer *hourHand;
CALayer *minHand;
CALayer *secHand;
NSTimer *timer;
}
//basic methods
- (void)start;
- (void)stop;
//customize appearence
- (void)setHourHandImage:(CGImageRef)image;
- (void)setMinHandImage:(CGImageRef)image;
- (void)setSecHandImage:(CGImageRef)image;
- (void)setClockBackgroundImage:(CGImageRef)image;
//to customize hands size: adjust following values in .m file
//HOURS_HAND_LENGTH
//MIN_HAND_LENGTH
//SEC_HAND_LENGTH
//HOURS_HAND_WIDTH
//MIN_HAND_WIDTH
//SEC_HAND_WIDTH
@endClockView.m
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197//
// ClockView.m
// clock
//
// Created by Ignacio Enriquez Gutierrez on 1/31/11.
// Copyright 2011 Nacho4D. All rights reserved.
// See the file License.txt for copying permission.
//
#import "ClockView.h"
@implementation ClockView
#pragma mark - Public Methods
- (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateClock:) userInfo:nil repeats:YES];
}
- (void)stop
{
[timer invalidate];
timer = nil;
}
//customize appearence
- (void)setHourHandImage:(CGImageRef)image
{
if (image == NULL) {
hourHand.backgroundColor = [UIColor grayColor].CGColor;
hourHand.cornerRadius = 3;
}else{
hourHand.backgroundColor = [UIColor clearColor].CGColor;
hourHand.cornerRadius = 0.0;
}
hourHand.contents = (id)image;
}
- (void)setMinHandImage:(CGImageRef)image
{
if (image == NULL) {
minHand.backgroundColor = [UIColor grayColor].CGColor;
}else{
minHand.backgroundColor = [UIColor clearColor].CGColor;
}
minHand.contents = (id)image;
}
- (void)setSecHandImage:(CGImageRef)image
{
if (image == NULL) {
secHand.backgroundColor = [UIColor whiteColor].CGColor;
secHand.borderWidth = 1.0;
secHand.borderColor = [UIColor redColor].CGColor;
}else{
secHand.backgroundColor = [UIColor clearColor].CGColor;
secHand.borderWidth = 0.0;
secHand.borderColor = [UIColor clearColor].CGColor;
}
secHand.contents = (id)image;
}
- (void)setClockBackgroundImage:(CGImageRef)image
{
if (image == NULL) {
containerLayer.borderColor = [UIColor blackColor].CGColor;
containerLayer.borderWidth = 1.0;
containerLayer.cornerRadius = 5.0;
}else{
containerLayer.borderColor = [UIColor clearColor].CGColor;
containerLayer.borderWidth = 0.0;
containerLayer.cornerRadius = 0.0;
}
containerLayer.contents = (id)image;
}
#pragma mark - Private Methods
//Default sizes of hands:
//in percentage (0.0 - 1.0)
#define HOURS_HAND_LENGTH 0.65
#define MIN_HAND_LENGTH 0.75
#define SEC_HAND_LENGTH 0.8
//in pixels
#define HOURS_HAND_WIDTH 1
#define MIN_HAND_WIDTH 1
#define SEC_HAND_WIDTH 1
float Degrees2Radians(float degrees) { return degrees * M_PI / 180; }
//timer callback
- (void) updateClock:(NSTimer *)theTimer{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:[NSDate date]];
NSInteger seconds = [dateComponents second];
NSInteger minutes = [dateComponents minute];
NSInteger hours = [dateComponents hour];
//NSLog(@"raw: hours:%d min:%d secs:%d", hours, minutes, seconds);
if (hours > 12) hours -=12; //PM
//set angles for each of the hands
CGFloat secAngle = Degrees2Radians(seconds/60.0*360);
CGFloat minAngle = Degrees2Radians(minutes/60.0*360);
CGFloat hourAngle = Degrees2Radians(hours/12.0*360) + minAngle/12.0;
//reflect the rotations + 180 degres since CALayers coordinate system is inverted
secHand.transform = CATransform3DMakeRotation (secAngle+M_PI, 0, 0, 1);
minHand.transform = CATransform3DMakeRotation (minAngle+M_PI, 0, 0, 1);
hourHand.transform = CATransform3DMakeRotation (hourAngle+M_PI, 0, 0, 1);
}
#pragma mark - Overrides
- (void) layoutSubviews
{
[super layoutSubviews];
containerLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
float length = MIN(self.frame.size.width, self.frame.size.height)/2;
CGPoint c = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
hourHand.position = minHand.position = secHand.position = c;
CGFloat w, h;
if (hourHand.contents == NULL){
w = HOURS_HAND_WIDTH;
h = length*HOURS_HAND_LENGTH;
}else{
w = CGImageGetWidth((CGImageRef)hourHand.contents);
h = CGImageGetHeight((CGImageRef)hourHand.contents);
}
hourHand.bounds = CGRectMake(0,0,w,h);
if (minHand.contents == NULL){
w = MIN_HAND_WIDTH;
h = length*MIN_HAND_LENGTH;
}else{
w = CGImageGetWidth((CGImageRef)minHand.contents);
h = CGImageGetHeight((CGImageRef)minHand.contents);
}
minHand.bounds = CGRectMake(0,0,w,h);
if (secHand.contents == NULL){
w = SEC_HAND_WIDTH;
h = length*SEC_HAND_LENGTH;
}else{
w = CGImageGetWidth((CGImageRef)secHand.contents);
h = CGImageGetHeight((CGImageRef)secHand.contents);
}
secHand.bounds = CGRectMake(0,0,w,h);
hourHand.anchorPoint = CGPointMake(0.5,0.0);
minHand.anchorPoint = CGPointMake(0.5,0.0);
secHand.anchorPoint = CGPointMake(0.5,0.0);
containerLayer.anchorPoint = CGPointMake(0.5, 0.5);
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
containerLayer = [[CALayer layer] retain];
hourHand = [[CALayer layer] retain];
minHand = [[CALayer layer] retain];
secHand = [[CALayer layer] retain];
//default appearance
[self setClockBackgroundImage:NULL];
[self setHourHandImage:NULL];
[self setMinHandImage:NULL];
[self setSecHandImage:NULL];
//add all created sublayers
[containerLayer addSublayer:hourHand];
[containerLayer addSublayer:minHand];
[containerLayer addSublayer:secHand];
[self.layer addSublayer:containerLayer];
}
return self;
}
- (void)dealloc
{
[self stop];
[hourHand release];
[minHand release];
[secHand release];
[containerLayer release];
[super dealloc];
}
@end
#####3. Objective-C 版本 BEMAnalogClock
功能较为完善版本,可以手动拨动指针实现动态修改时间,如果需要实现较为复杂的时钟可考虑使用该版本
效果图
(无)
备注
(无)
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 宁静至远,博雅多通!