博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
变换CALayer锚点实现模拟时钟的动画
阅读量:7143 次
发布时间:2019-06-29

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

变换CALayer锚点实现模拟时钟的动画

 

变换锚点得需要一点理论知识,看下图就能明白:).

开始实现模拟时钟效果:

////  RootViewController.m////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "YXGCD.h"@interface RootViewController ()@property (nonatomic, strong) GCDTimer *timer;@end// 将角度转换为弧度#define DEGREES__TO__RADIANS(d)  ((d) * M_PI / 180.f)@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];        // 显示参考用的view    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 300, 300)];    showView.layer.borderWidth = 1.f;    showView.layer.cornerRadius = 150.f;    showView.layer.borderColor = [UIColor redColor].CGColor;    showView.center = self.view.center;    [self.view addSubview:showView];        // 新建layer    CALayer *layer = [CALayer layer];    layer.backgroundColor = [UIColor blackColor].CGColor;        // 重置锚点    layer.anchorPoint = CGPointMake(0.f, 0.f);        // 设置layer的frame值(在showView正中间摆放)    layer.frame = CGRectMake(showView.middleX, showView.middleY, 1, 150);        // 添加进showView中    [showView.layer addSublayer:layer];        // 定时器    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];    [_timer event:^{        static int i = 1;                // 每秒增加的角度        layer.transform = \            CATransform3DMakeRotation(DEGREES__TO__RADIANS((360/60.f) * i++), 0.0, 0.0, 1.0);    } timeInterval:NSEC_PER_SEC];    [_timer start];}@end

 

重要的代码:

以下是最终效果:

完整代码:

////  RootViewController.m////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "YXGCD.h"static NSDateFormatter* _DMLogDateFormatter = nil;@interface RootViewController ()@property (nonatomic, strong) GCDTimer *timer;@property (nonatomic, strong) UILabel  *timeLabel;@end// 将角度转换为弧度#define DEGREES__TO__RADIANS(d)  ((d) * M_PI / 180.f)@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor blackColor];        // 日期格式    _DMLogDateFormatter = [[NSDateFormatter alloc] init];    [_DMLogDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];    [_DMLogDateFormatter setDateFormat:@"HH:mm:ss"];        // 显示label    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];    _timeLabel.textAlignment = NSTextAlignmentCenter;    _timeLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:29.f];    _timeLabel.textColor = [UIColor cyanColor];    _timeLabel.center = self.view.center;    _timeLabel.y += 190;    [self.view addSubview:_timeLabel];        // 显示参考用的view    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 300, 300)];    showView.layer.borderWidth = 1.f;    showView.layer.cornerRadius = 150.f;    showView.layer.borderColor = [UIColor redColor].CGColor;    showView.center = self.view.center;    [self.view addSubview:showView];        // 新建秒钟Layer    // ----------------------------------------------------- //    CALayer *secondLayer = [CALayer layer];    secondLayer.backgroundColor = [UIColor whiteColor].CGColor;        // 重置锚点    secondLayer.anchorPoint = CGPointMake(0.f, 0.f);        // 设置layer的frame值(在showView正中间摆放)    secondLayer.frame = CGRectMake(showView.middleX, showView.middleY, 1, 140);        // 添加进showView中    [showView.layer addSublayer:secondLayer];            // 新建分钟Layer    // ----------------------------------------------------- //    CALayer *minuteLayer = [CALayer layer];    minuteLayer.backgroundColor = [UIColor greenColor].CGColor;        // 重置锚点    minuteLayer.anchorPoint = CGPointMake(0.f, 0.f);        // 设置layer的frame值(在showView正中间摆放)    minuteLayer.frame = CGRectMake(showView.middleX, showView.middleY, 1, 120);        // 添加进showView中    [showView.layer addSublayer:minuteLayer];            // 新建时钟Layer    // ----------------------------------------------------- //    CALayer *hourLayer = [CALayer layer];    hourLayer.backgroundColor = [UIColor blueColor].CGColor;        // 重置锚点    hourLayer.anchorPoint = CGPointMake(0.f, 0.f);        // 设置layer的frame值(在showView正中间摆放)    hourLayer.frame = CGRectMake(showView.middleX, showView.middleY, 1, 100);        // 添加进showView中    [showView.layer addSublayer:hourLayer];            // 定时器    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];    [_timer event:^{                NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];        NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];        // 获取到时间        float sec =  [timeArray[2] intValue];        float min =  [timeArray[1] intValue] + sec / 60.f;        float hour = [timeArray[0] intValue] + min / 60.f;                secondLayer.transform = \            CATransform3DMakeRotation(DEGREES__TO__RADIANS(360/60.f)*sec + \                                      DEGREES__TO__RADIANS(180), \                                      0.0, 0.0, 1.0);                minuteLayer.transform = \        CATransform3DMakeRotation(DEGREES__TO__RADIANS(360/60.f)*min + \                                  DEGREES__TO__RADIANS(180), \                                  0.0, 0.0, 1.0);                hourLayer.transform = \        CATransform3DMakeRotation(DEGREES__TO__RADIANS(360/24.f)*hour + \                                  DEGREES__TO__RADIANS(360), \                                  0.0, 0.0, 1.0);                _timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",                           [timeArray[0] intValue],                           [timeArray[1] intValue],                           [timeArray[2] intValue]];            } timeInterval:NSEC_PER_SEC];    [_timer start];}@end
RootViewController.m

 

 

 

转载地址:http://bsgrl.baihongyu.com/

你可能感兴趣的文章
ElasticSearch操作实例大全---文档结构操作(2)
查看>>
服务器===集群和分布式的区别是什么?
查看>>
win平台常用cmd命令小结
查看>>
Fiddler4无法抓取HttpWebRequest本地请求的解决办法
查看>>
以C语言为例的程序性能优化 --《深入理解计算机系统》第五章读书笔记
查看>>
Nginx 常见应用技术指南[Nginx Tips] 第二版(本文为转载)
查看>>
CURL的使用<发送与接收数据>
查看>>
poj2182
查看>>
《构建之法》第8,9,10章
查看>>
认识flex的启动、初始化
查看>>
listagg乱码问题
查看>>
调用Android系统设置项
查看>>
Windows中使用MySQL 创建数据库(二)
查看>>
随笔:易经句摘
查看>>
Hibernate-ORM:13.Hibernate中的连接查询
查看>>
CentOS7的/tmp目录自动清理规则
查看>>
Senior Manufacturing Technical Manager
查看>>
Spring IOC 理解
查看>>
poj2551
查看>>
day8:vcp考试
查看>>