iOS实现漂亮的时钟代码

iOS实现漂亮的时钟代码,第1张

概述iOS实现漂亮的时钟代码

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

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,1);    self.layerMin.transform = CAtransform3DMakeRotation(CURRENTMIN * kPerMinuteA,1);    self.layerHour.transform = CAtransform3DMakeRotation(CURRENTHOUR * kPerHourA,1);    self.layerHour.transform = CAtransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR*kPerHourA,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,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,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,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程序

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的iOS实现漂亮的时钟代码全部内容,希望文章能够帮你解决iOS实现漂亮的时钟代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106146.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存