标签:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) CALayer *hourLayer;
@property (nonatomic,strong) CALayer *minLayer;
@property (nonatomic,strong) CALayer *secLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建时针,分针,秒针;
self.hourLayer= [CALayer layer];
_hourLayer.backgroundColor = [UIColor blackColor].CGColor;
_hourLayer.bounds = CGRectMake(0, 0, 4, 40);
_hourLayer.position = self.view.center;
_hourLayer.anchorPoint = CGPointMake(0.5, 0.8);
[self.view.layer addSublayer:_hourLayer];
self.minLayer = [CALayer layer];
_minLayer.backgroundColor = [UIColor blackColor].CGColor;
_minLayer.bounds = CGRectMake(0, 0, 2, 70);
_minLayer.position = self.view.center;
_minLayer.anchorPoint = CGPointMake(0.5, 0.8);
[self.view.layer addSublayer:_minLayer];
self.secLayer = [CALayer layer];
_secLayer.backgroundColor = [UIColor redColor].CGColor;
_secLayer.bounds = CGRectMake(0, 0, 2, 90);
_secLayer.position = self.view.center;
_secLayer.anchorPoint = CGPointMake(0.5, 0.8);
[self.view.layer addSublayer:_secLayer];
//创建中心点
CALayer *centerLayer = [CALayer layer];
centerLayer.bounds = CGRectMake(0, 0 , 8, 8);
centerLayer.cornerRadius = 4;
centerLayer.backgroundColor = [UIColor blackColor].CGColor;
centerLayer.position = self.view.center;
[self.view.layer addSublayer:centerLayer];
//获取当前时间
NSDate *nowDate = [NSDate date];
NSDateFormatter *formater = [NSDateFormatter new];
formater.dateFormat = @"K:m:s";
NSString *dateString = [formater stringFromDate:nowDate];
//对时间按照:进行分离
NSArray *numsArray = [dateString componentsSeparatedByString:@":"];
//取出数组中的数字,设置对应的时分秒针旋转度数
int hour = [[numsArray objectAtIndex:0]intValue];
int min = [[numsArray objectAtIndex:1]intValue];
int sec = [[numsArray objectAtIndex:2]intValue];
//时针
_hourLayer.transform = CATransform3DRotate(_hourLayer.transform, (hour + min/60.0)*30.0/180.0*M_PI , 0, 0, 1);
//分针
_minLayer.transform = CATransform3DRotate(_minLayer.transform, (min + sec/60.0)*6.0/180.0*M_PI, 0, 0, 1);
//秒针
_secLayer.transform = CATransform3DRotate(_secLayer.transform, sec*6/180.0 *M_PI, 0, 0, 1);
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
}
//旋转
-(void)rotate{
_secLayer.transform = CATransform3DRotate(_secLayer.transform, 6/180.0 * M_PI, 0, 0, 1);
_minLayer.transform = CATransform3DRotate(_minLayer.transform, (6/60.0)/180.0 *M_PI, 0, 0, 1);
_hourLayer.transform = CATransform3DRotate(_hourLayer.transform, (30/3600.0)/180.0 *M_PI, 0, 0, 1);
}
@end
标签:
原文地址:http://www.cnblogs.com/damonWq/p/5158564.html