#import "ViewController.h"
//每一秒旋转多少度
#define perSecA6
//每一分旋转多少度
#define perMinA6
//每一小时旋转多少度
#define perHourA30
//每一分,时针旋转的度数
#define perMinHour0.5
#define angle2Rad(angle) ((angle) /180* M_PI)
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *clockView;
@property (nonatomic, weak) CALayer *secL;
@property (nonatomic, weak) CALayer *minL;
@property (nonatomic, weak) CALayer *hourL;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[selfsetHour];
[selfsetMin];
[selfsetSec];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
[self timeChange];
}
-(void)timeChange{
NSCalendar *cal = [NSCalendar currentCalendar];
//components:日历组件,年,月,日,时,分,秒
//fromDate:从什么时间开始获取
NSDateComponents *cmp = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
//获取当前多少秒
NSIntegercurSec = cmp.second;
//秒针旋转
//计算秒针当前旋转的角度
//angle:当前多少秒 * 每一秒旋转多少度
CGFloatsecA = curSec *perSecA;
self.secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1);
//获取当前多少分
NSIntegercurMin = cmp.minute;
//分针开始旋转
//计算分针当前旋转的角度
//angle = 当前多少分 * 每一分旋转多少度
CGFloatminA = curMin *perMinA;
self.minL.transform = CATransform3DMakeRotation((minA), 0, 0, 1);
//获取当前多少小时
NSIntegercurHour = cmp.hour;
//时针开始旋转
//计算时针当前旋转的角度
//angle = 当前多少小时 * 每一分旋转多少度
CGFloathourA = curHour *perHourA+ curMin *perMinHour;
self.hourL.transform = CATransform3DMakeRotation(angle2Rad(hourA), 0, 0, 1);
}
-(void)setSec{
CALayer*secL = [CALayerlayer];
secL.bounds=CGRectMake(0,0,3,70);
secL.backgroundColor = [UIColor redColor].CGColor;
secL.anchorPoint=CGPointMake(0.5,1);
secL.cornerRadius=0.5;
secL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
[self.clockView.layeraddSublayer:secL];
self.secL= secL;
}
-(void)setMin{
CALayer*minL = [CALayerlayer];
minL.bounds=CGRectMake(0,0,3,60);
minL.backgroundColor = [UIColor blackColor].CGColor;
minL.anchorPoint=CGPointMake(0.5,1);
minL.cornerRadius=0.5;
minL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
[self.clockView.layeraddSublayer:minL];
self.minL= minL;
}
-(void)setHour{
CALayer*hourL = [CALayerlayer];
hourL.bounds=CGRectMake(0,0,3,40);
hourL.backgroundColor = [UIColor blackColor].CGColor;
hourL.anchorPoint=CGPointMake(0.5,1);
hourL.cornerRadius=0.5;
hourL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
[self.clockView.layeraddSublayer:hourL];
self.hourL= hourL;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
//所有旋转,缩放,都是绕着锚点进行
self.secL.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论