1.最终效果图
2.实现思路
- 在ios中默认是绕着中心点旋转的,因为锚点默认在图层的中点,要想绕着下边中心点转,需要改变图层锚点的位置。
- 根据锚点,设置position坐标,为时钟的中点。
- 思考秒针旋转的角度,怎么知道当前秒针旋转到哪,当前秒针旋转的角度 = 当前秒数 * 每秒转多少°。
1> 计算一秒转多少° 360 * 60 = 6
2> 获取当前秒数,通过日历对象,获取日期组成成分 nscalendar -> nsdatecomponents -> 获取当前秒数 - 每隔一秒,获取最新秒数,更新时钟。
- 分钟一样的做法
- 时钟也一样
- 每一分钟,时钟也需要旋转,60分钟 -> 1小时 - > 30° ==》 每分钟 30 / 60.0 一分钟时针转0.5°
- 把时针和秒针头尾变尖,设置圆角半径
2.完整代码
#import "viewcontroller.h"
//获得当前的年月日 时分秒
#define currentsec [[nscalendar currentcalendar] component:nscalendarunitsecond fromdate:[nsdate date]]
#define currentmin [[nscalendar currentcalendar] component:nscalendarunitminute fromdate:[nsdate date]]
#define currenthour [[nscalendar currentcalendar] component:nscalendarunithour fromdate:[nsdate date]]
#define currentday [[nscalendar currentcalendar] component:nscalendarunitday fromdate:[nsdate date]]
#define currentmonth [[nscalendar currentcalendar] component:nscalendarunitmonth fromdate:[nsdate date]]
#define currentyear [[nscalendar currentcalendar] component:nscalendarunityear fromdate:[nsdate date]]
//角度转换成弧度
#define angel(x) x/180.0 * m_pi
#define kperseconda angel(6)
#define kperminutea angel(6)
#define kperhoura angel(30)
#define kperhourminutea angel(0.5)
@interface viewcontroller ()
@property (nonatomic,strong) uiimageview *imageclock;
@property (nonatomic,strong) calayer *layersec;
@property (nonatomic,strong) calayer *layermin;
@property (nonatomic,strong) calayer *layerhour;
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
[self.view addsubview:self.imageclock];
[self.imageclock.layer addsublayer:self.layersec];
[self.imageclock.layer addsublayer:self.layermin];
[self.imageclock.layer addsublayer:self.layerhour];
[self timechange];
[nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timechange) userinfo:nil repeats:yes];
// do any additional setup after loading the view, typically from a nib.
}
- (void)timechange
{
self.layersec.transform = catransform3dmakerotation(currentsec * kperseconda, 0, 0, 1);
self.layermin.transform = catransform3dmakerotation(currentmin * kperminutea, 0, 0, 1);
self.layerhour.transform = catransform3dmakerotation(currenthour * kperhoura, 0, 0, 1);
self.layerhour.transform = catransform3dmakerotation(currentmin * kperhourminutea currenthour*kperhoura, 0, 0, 1);
}
- (uiimageview *)imageclock
{
if (_imageclock == nil) {
_imageclock = [[uiimageview alloc]initwithimage:[uiimage imagenamed:@"钟表"]];
_imageclock.frame = cgrectmake(100, 100, 200, 200);
}
return _imageclock;
}
- (calayer *)layersec
{
if (_layersec == nil) {
_layersec = [calayer layer];
_layersec.bounds = cgrectmake(0, 0, 2, 80);
_layersec.backgroundcolor = [uicolor redcolor].cgcolor;
_layersec.cornerradius = 5;
_layersec.anchorpoint = cgpointmake(0.5, 1);
_layersec.position = cgpointmake(self.imageclock.bounds.size.width/2, self.imageclock.bounds.size.height/2);
}
return _layersec;
}
- (calayer *)layermin
{
if (_layermin == nil) {
_layermin = [calayer layer];
_layermin.bounds = cgrectmake(0, 0, 4, 60);
_layermin.backgroundcolor = [uicolor blackcolor].cgcolor;
_layermin.cornerradius = 5;
_layermin.anchorpoint = cgpointmake(0.5, 1);
_layermin.position = cgpointmake(self.imageclock.bounds.size.width/2, self.imageclock.bounds.size.height/2);
}
return _layermin;
}
- (calayer *)layerhour
{
if (_layerhour == nil) {
_layerhour = [calayer layer];
_layerhour.bounds = cgrectmake(0, 0, 6, 40);
_layerhour.backgroundcolor = [uicolor blackcolor].cgcolor;
_layerhour.cornerradius = 5;
_layerhour.anchorpoint = cgpointmake(0.5, 1);
_layerhour.position = cgpointmake(self.imageclock.bounds.size.width/2, self.imageclock.bounds.size.height/2);
}
return _layerhour;
}
3.demo程序
https://github.com/esdeath/clock
相机定格咔嚓