Demo5—时钟
1.使用图层设置背景图片
self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"time"].CGImage);
2.创建一个图层用来显示时钟图片,并设置相应的属性
//创建一个图层用来显示一张图片
CALayer *wheelLayer = [CALayer layer];//设置显示的位置position
wheelLayer.position = CGPointMake(180, 250);//设置图层的宽 高 bounds属性
wheelLayer.bounds = CGRectMake(0, 0, 296, 296);//设置锚点:图层的哪个位置和position重合
wheelLayer.anchorPoint = CGPointMake(0.5, 0.5);//设置图层的内容 即图片
wheelLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"Clock"].CGImage);//图层没法接收事件 要添加图层必须依赖某个视图 所以需要将图层添加到某个视图上图层上
[self.view.layer addSublayer:wheelLayer];
3.创建一个点图层
//创建一个圆点图层
CALayer *redPointLayer = [CALayer layer];//让红点和表环的中心点重合
redPointLayer.position = wheelLayer.position;//设置圆点的尺寸
redPointLayer.bounds = CGRectMake(0, 0, 15, 15);//设置圆角半径
redPointLayer.cornerRadius = 7.5;//设置圆点颜色
redPointLayer.backgroundColor = [UIColor redColor].CGColor;
4.创建指针,并设置相应属性
- 创建时针
//创建时针
self.hourLayer = [CALayer layer];
//位置
_hourLayer.position = wheelLayer.position;
//设置锚点
_hourLayer.anchorPoint = CGPointMake(0.5, 0.7);
//尺寸
_hourLayer.bounds = CGRectMake(0, 0, 8, 80);
//颜色
_hourLayer.backgroundColor = [UIColor blackColor].CGColor;
//显示
[self.view.layer addSublayer:_hourLayer];
- 创建分针
//创建分针
self.miniuteLayer = [CALayer layer];
//位置
_miniuteLayer.position = wheelLayer.position;
//设置锚点
_miniuteLayer.anchorPoint = CGPointMake(0.5, 0.7);
//尺寸
_miniuteLayer.bounds = CGRectMake(0, 0, 6, 100);
//颜色
_miniuteLayer.backgroundColor = [UIColor redColor].CGColor;
//显示
[self.view.layer addSublayer:_miniuteLayer];
- 创建秒针
//创建秒针
self.secondLayer = [CALayer layer];
//位置
_secondLayer.position = wheelLayer.position;
//设置锚点
_secondLayer.anchorPoint = CGPointMake(0.5, 0.7);
//尺寸
_secondLayer.bounds = CGRectMake(0, 0, 4, 120);
//颜色
_secondLayer.backgroundColor = [UIColor greenColor].CGColor;
//显示
[self.view.layer addSublayer:_secondLayer];
5.写一个方法,让指针对应当前的具体时间
- (void)changeTime{//1.获取当前的时间,时间的类:NSDate,二进制:NSDataNSDate *nowTime = [NSDate date];//提取时间,NSDateFormatter类:管理读取时间的格式NSDateFormatter *formatter = [[NSDateFormatter alloc] init];//设置提取的格式formatter.dateFormat = @"K:m:s";//从当前时间中提取时分秒NSString *timeStr = [formatter stringFromDate:nowTime];NSLog(@"%@", timeStr);//将字符串分割NSArray *timeArray = [timeStr componentsSeparatedByString:@":"];NSLog(@"%@", timeArray);//将数组中的字符串转换成整型int hour = [[timeArray objectAtIndex:0] intValue];int minute = [[timeArray objectAtIndex:1] intValue];int second = [[timeArray objectAtIndex:2] intValue];NSLog(@"%d %d %d", hour, minute, second);//旋转时分秒针_hourLayer.transform = CATransform3DRotate(self.hourLayer.transform, M_PI/6*(hour+minute/60), 0, 0, 1);_miniuteLayer.transform = CATransform3DRotate(self.miniuteLayer.transform, M_PI/30*(minute+second/60), 0, 0, 1);_secondLayer.transform = CATransform3DRotate(self.secondLayer.transform, M_PI/30.0*second, 0, 0, 1);
}
6.开启一个定时器,使程序运行起来,时钟开始走
- (void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated];//开启定时器,让时钟走起来//1.CADisplayLink - 对UIView进行动画 无法完成只运行某个时间段的设置//2.NSTime - 数据的处理 如果视图中有滚动视图 在执行滚动事件的时候定时器无效了CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate)];//默认情况下通过屏幕刷新pinlv来执行任务 60HZ:1s刷新屏幕60次link.preferredFramesPerSecond = 1;//必须将这个对象放到NSRunLoop中才能被执行[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
7.写一个方法,实现让时分秒指针转起来的操作
- (void)rotate{_secondLayer.transform = CATransform3DRotate(_secondLayer.transform, M_PI*2/60.0, 0, 0, 1);_miniuteLayer.transform = CATransform3DRotate(_miniuteLayer.transform, M_PI*2/60.0/60.0, 0, 0, 1);_hourLayer.transform = CATransform3DRotate(_hourLayer.transform, M_PI*2/60.0/60.0/12.0, 0, 0, 1);
}
8.添加"嘀嗒"的钟表声
- 先导入头文件
#import <AudioToolbox/AudioToolbox.h>
- 实现方法
- (void)playMusic{//1.获取音频文件路径NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dida.m4a" ofType:nil];//2.将字符串的路径转化为NSURL类型NSURL *url = [NSURL fileURLWithPath:filePath];//3.为这个url对应的音频,创建一个系统IDSystemSoundID soundID;AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);//4.播放音频AudioServicesPlaySystemSound(soundID);
}