博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
纯代码绘制时钟
阅读量:6573 次
发布时间:2019-06-24

本文共 7663 字,大约阅读时间需要 25 分钟。

hot3.png

////  ViewController.m//  time////#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) UIView *clockView;@property (nonatomic,strong) CALayer *layerSec;@property (nonatomic,strong) CALayer *layerMin;@property (nonatomic,strong) CALayer *layerHour;@property (nonatomic,strong) CALayer *layerSec2;@property (nonatomic,strong) CALayer *layerMin2;@property (nonatomic,strong) CALayer *layerHour2;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor grayColor];    // Do any additional setup after loading the view, typically from a nib.    [self.view addSubview:self.clockView];    [self.clockView.layer addSublayer:self.layerSec];    [self.clockView.layer addSublayer:self.layerMin];    [self.clockView.layer addSublayer:self.layerHour];    [self.view addSubview:self.imageClock];    [self.imageClock.layer addSublayer:self.layerSec2];    [self.imageClock.layer addSublayer:self.layerMin2];    [self.imageClock.layer addSublayer:self.layerHour2];    [self timeChange];    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];}- (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);    self.layerSec2.transform = CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1);    self.layerMin2.transform = CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);//    self.layerHour2.transform = CATransform3DMakeRotation(CURRENTHOUR * kPerHourA, 0, 0, 1);    self.layerHour2.transform = CATransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR * kPerHourA, 0, 0, 1);}- (UIImageView *)imageClock {    if (_imageClock == nil) {        _imageClock = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"钟表"]];        _imageClock.frame = CGRectMake(100, 320, 200, 200);        _imageClock.layer.cornerRadius = 45;        _imageClock.layer.masksToBounds = YES;    }    return _imageClock;}- (CALayer *)layerSec {    if (_layerSec == nil) {        _layerSec = [CALayer layer];        _layerSec.bounds = CGRectMake(0, 0, 2, 100);        _layerSec.backgroundColor = [UIColor redColor].CGColor;        _layerSec.cornerRadius = 5;        _layerSec.anchorPoint = CGPointMake(0.5, 0.9);// 固定点        _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, 70);        _layerMin.backgroundColor = [UIColor blackColor].CGColor;        _layerMin.cornerRadius = 5;        _layerMin.anchorPoint = CGPointMake(0.5, 0.95);        _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, 50);        _layerHour.backgroundColor = [UIColor blackColor].CGColor;        _layerHour.cornerRadius = 5;        _layerHour.anchorPoint = CGPointMake(0.5, 0.95);        _layerHour.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);    }    return _layerHour;}- (CALayer *)layerSec2 {    if (_layerSec2 == nil) {        _layerSec2 = [CALayer layer];        _layerSec2.bounds = CGRectMake(0, 0, 2, 100);        _layerSec2.backgroundColor = [UIColor redColor].CGColor;        _layerSec2.cornerRadius = 5;        _layerSec2.anchorPoint = CGPointMake(0.5, 0.9);// 固定点        _layerSec2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);    }    return _layerSec2;}- (CALayer *)layerMin2 {    if (_layerMin2 == nil) {        _layerMin2 = [CALayer layer];        _layerMin2.bounds = CGRectMake(0, 0, 4, 70);        _layerMin2.backgroundColor = [UIColor blackColor].CGColor;        _layerMin2.cornerRadius = 5;        _layerMin2.anchorPoint = CGPointMake(0.5, 0.95);        _layerMin2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);    }    return _layerMin2;}- (CALayer *)layerHour2 {    if (_layerHour2 == nil) {        _layerHour2 = [CALayer layer];        _layerHour2.bounds = CGRectMake(0, 0, 6, 50);        _layerHour2.backgroundColor = [UIColor blackColor].CGColor;        _layerHour2.cornerRadius = 5;        _layerHour2.anchorPoint = CGPointMake(0.5, 0.95);        _layerHour2.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);    }    return _layerHour2;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (UIView *)clockView {    if (_clockView == nil) {        _clockView = [[UIView alloc] initWithFrame:CGRectMake(100, 80, 200, 200)];        _clockView.backgroundColor = [UIColor whiteColor];        _clockView.layer.cornerRadius = 100;        _clockView.layer.masksToBounds = YES;        [self create];    }    return _clockView;}- (void)create {    for (int i = 1; i <= 60; i++) {        CALayer *layer = [CALayer layer];        CGFloat height;        if (i % 5 == 0) {            int j = (int)(i / 5);            height = 10;            layer.anchorPoint = CGPointMake(0.5, 10);// 固定点            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 10)];            label.textColor = [UIColor blackColor];            label.text = [NSString stringWithFormat:@"%d", j];            label.textAlignment = NSTextAlignmentCenter;            label.font = [UIFont systemFontOfSize:12];            label.layer.position = CGPointMake(_clockView.bounds.size.width/2, _clockView.bounds.size.height/2);            label.layer.transform = CATransform3DMakeRotation(i * kPerSecondA, 0, 0, 1);            label.layer.anchorPoint = CGPointMake(0.5, 8.5);            [_clockView addSubview:label];        } else {            height = 5;            layer.anchorPoint = CGPointMake(0.5, 20);// 固定点        }        layer.bounds = CGRectMake(0, 0, 4, height);        layer.backgroundColor = [UIColor blackColor].CGColor;        layer.position = CGPointMake(_clockView.bounds.size.width/2, _clockView.bounds.size.height/2);        layer.transform = CATransform3DMakeRotation(i * kPerSecondA, 0, 0, 1);        [_clockView.layer addSublayer:layer];    }}@end

 

转载于:https://my.oschina.net/zsyzone/blog/704231

你可能感兴趣的文章
Android 面试常问七道题
查看>>
TestNG介绍 - 1
查看>>
当前用户更改运行方式出现错误的解决办法
查看>>
visual studio 11开发Win8模板
查看>>
Linux下VsFTP和ProFTP用户管理高级技巧 之一
查看>>
Xamarin 技术全解析
查看>>
mySQL用户和权限管理v1
查看>>
烂泥:CentOS6.5光盘以及ISO镜像文件的使用
查看>>
awk入门-保护SSHD
查看>>
Word 2003中对不同的节设置不同的页面边框的特殊情况小结
查看>>
可视化就是简单化,可视化就是易用化
查看>>
绑定变量窥测
查看>>
使用Live Writer和NNTP Bridge阅读微软论坛
查看>>
HYPER-V RTM版安装
查看>>
Best MSI to EXE Convert tool
查看>>
SCCM 2012 SP1系列(一)先决条件准备-1
查看>>
实战:Windows Server 2008 活动目录 传送和争夺操作主控角色
查看>>
机器学习入门|线性回归(二)
查看>>
少侠的空间
查看>>
保存ResultSet中的数据(Java Source Code)
查看>>