自制圆形时钟⏰

如果想着做一个类似的家里的圆形时钟的样式的钟表,可以使用这样的方法:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic ,weak)CALayer *second;

@property (nonatomic ,weak)CALayer *minute;

@property (nonatomic ,weak)CALayer *hour;

@end

@implementation ViewController

  • (void)viewDidLoad {

**super** viewDidLoad\]; **self**.view.backgroundColor = \[UIColor cyanColor\]; //创建表盘 CALayer \*clock = \[\[CALayer alloc\]init\]; //大小 clock.bounds = CGRectMake(0, 0, 200, 200); //位置 clock.position = CGPointMake(200, 200); //内容 clock.contents = (**__bridge** **id** **_Nullable**)(\[UIImage imageNamed:@"clock"\].CGImage); \[**self**.view.layer addSublayer:clock\]; //圆角 clock.cornerRadius = 100; //裁剪 clock.masksToBounds = **YES**; //创建秒针 CALayer \*second = \[\[CALayer alloc\]init\]; **self**.second = second; //大小 second.bounds = CGRectMake(0, 0, 2, 100); //位置 second.position = clock.position; //颜色 second.backgroundColor = \[UIColor redColor\].CGColor; //锚点 second.anchorPoint = CGPointMake(0.5, 0.8); \[**self**.view.layer addSublayer:second\]; //创建分针 CALayer \*minute = \[\[CALayer alloc\]init\]; **self**.minute = minute; //大小 minute.bounds = CGRectMake(0, 0, 3, 80); //位置 minute.position = clock.position; //颜色 minute.backgroundColor = \[UIColor blueColor\].CGColor; //锚点 minute.anchorPoint = CGPointMake(0.5, 0.8); \[**self**.view.layer addSublayer:minute\]; //创建时针 CALayer \*hour = \[\[CALayer alloc\]init\]; **self**.hour = hour; //大小 hour.bounds = CGRectMake(0, 0, 4, 60); //位置 hour.position = clock.position; //颜色 hour.backgroundColor = \[UIColor purpleColor\].CGColor; //锚点 hour.anchorPoint = CGPointMake(0.5, 0.8); \[**self**.view.layer addSublayer:hour\]; CADisplayLink \*link = \[CADisplayLink displayLinkWithTarget:**self** selector:**@selector**(timeChange)\]; \[link addToRunLoop:\[NSRunLoop mainRunLoop\] forMode:NSDefaultRunLoopMode\]; \[**self** timeChange\]; } -(**void**)timeChange{ NSDate \*date = \[NSDate date\]; // // NSDateFormatter \*formatter = \[\[NSDateFormatter alloc\]init\]; // formatter.dateFormat = @"ss"; // NSString \*ms = \[formatter stringFromDate:date\]; // CGFloat msValue = \[ms floatValue\]; NSCalendar \*cal = \[NSCalendar currentCalendar\]; CGFloat ms = \[cal component:NSCalendarUnitNanosecond fromDate:date\]; CGFloat s= \[cal component:NSCalendarUnitSecond fromDate:date\]; CGFloat m = \[cal component:NSCalendarUnitMinute fromDate:date\]; CGFloat h = \[cal component:NSCalendarUnitHour fromDate:date\]; //秒针角度 CGFloat angle = (2 \* M_PI/60) / 1000000000; CGFloat angle1 = 2 \* M_PI / 60; **self**.second.affineTransform = CGAffineTransformMakeRotation(angle1 \* s + angle \* ms); //分针角度 CGFloat minuteAngle = 2 \* M_PI / 60; **self**.minute.affineTransform = CGAffineTransformMakeRotation(minuteAngle \* m + angle1 \* s / 60 + angle \* ms / 60); //时针角度 CGFloat hourAngle = 2 \* M_PI / 12; **self**.hour.affineTransform = CGAffineTransformMakeRotation(hourAngle \* h + minuteAngle \* m / 12 + angle1 \* s / 60 + angle \* ms / 60); } - (**void**)didReceiveMemoryWarning { \[**super** didReceiveMemoryWarning\]; // Dispose of any resources that can be recreated. } **@end 当然还要有一个这样的表盘,** ![](https://file.jishuzhan.net/article/1721764295437455362/b44ce457bb5194d593c49f2c48cd168a.webp) 其实代码实现主要是用的CALayer的一些动画效果实现的。

相关推荐
布多1 小时前
Tagged Pointer:苹果工程师的内存优化艺术
ios·源码
Rudon滨海渔村2 小时前
新旧iPhone相册复制 - 相册图片视频对拷 - 换机 - 迁移设备数据 - 免费开源爱思助手
ios·iphone
清晨細雨16 小时前
UniApp集成极光推送详细教程
android·ios·uni-app·极光推送
ii_best21 小时前
iOS 按键越狱脚本支持一键新机软件教程
ios
lilili啊啊啊1 天前
查看iphone手机的使用记录-克魔实战
ios·智能手机·iphone
鸿蒙布道师1 天前
鸿蒙NEXT开发随机工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
鸿蒙布道师2 天前
鸿蒙NEXT开发Base64工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
美狐美颜sdk2 天前
美颜SDK兼容性挑战:如何让美颜滤镜API适配iOS与安卓?
android·深度学习·ios·美颜sdk·第三方美颜sdk·视频美颜sdk
Invisible_He2 天前
iOS自定义collection view的page size(width/height)分页效果
ui·ios·swift·collection
小橙子20772 天前
一条命令配置移动端(Android / iOS)自动化环境
android·ios·自动化