主要视图结构:
@property (weak, nonatomic) IBOutlet UIImageView *clockView;
@property (weak,nonatomic) CALayer *secondLayer;
@property (weak,nonatomic) CALayer *minuteLayer;
@property (weak,nonatomic) CALayer *hourLayer;
主要代码实现
- (void)viewDidLoad {
[super viewDidLoad];
[self layerRotation];
//开启定时器
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(layerRotation) userInfo:nil repeats:YES];
}
#pragma mark - 秒针旋转
- (void)layerRotation {
//获取秒数
NSCalendar *calendar = [NSCalendar currentCalendar];//获取日历对象
NSDateComponents *components = [calendar components:NSCalendarUnitSecond| NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]];
CGFloat second = components.second;
//计算当前的秒针对应的弧度
CGFloat secondAngle = second *KHSPerSecondAngle;
//1、旋转秒针
[self.secondLayer setTransform:CATransform3DMakeRotation(angle2radian(secondAngle), 0, 0, 1)];
//KVC--有bug
// [self.secondLayer setValue:@angle2radian(secondAngle) forKeyPath:@"transform.rotation"];
//2、旋转分针
CGFloat minute = components.minute;
CGFloat minuteAngle = minute *KHSPerMinuteAngle +second*(KHSPerMinuteAngle/60.0);//整分+秒的弧度(每秒对应的0.1弧度)
[self.minuteLayer setTransform:CATransform3DMakeRotation(angle2radian(minuteAngle), 0, 0, 1)];
//3、旋转时针
CGFloat hour = components.hour;
//计算当前的秒针对应的弧度
CGFloat hourAngle = hour *KHSPerHourAngle + minute*(KHSPerHourAngle/60.0) +second*(KHSPerHourAngle/360.0);//整点的角度+分钟的角度+秒的弧度(2分钟=1弧度=》1个小时=30弧度)
[self.hourLayer setTransform:CATransform3DMakeRotation(angle2radian(hourAngle), 0, 0, 1)];
}
作者:u011018979 发表于2017/7/2 17:00:56 原文链接
阅读:62 评论:0 查看评论