#import "ViewController.h"
//每一秒旋轉(zhuǎn)多少度
#define? perSecA6
//每一分旋轉(zhuǎn)多少度
#define perMinA6
//每一小時(shí)旋轉(zhuǎn)多少度
#define perHourA30
//每一分,時(shí)針旋轉(zhuǎn)的度數(shù)
#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:日歷組件,年,月,日,時(shí),分,秒
? ? //fromDate:從什么時(shí)間開始獲取
? ? NSDateComponents *cmp = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
? ? //獲取當(dāng)前多少秒
? ? NSIntegercurSec = cmp.second;
? ? //秒針旋轉(zhuǎn)
? ? //計(jì)算秒針當(dāng)前旋轉(zhuǎn)的角度
? ? //angle:當(dāng)前多少秒 * 每一秒旋轉(zhuǎn)多少度
? ? CGFloatsecA = curSec *perSecA;
? ? self.secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1);
? ? //獲取當(dāng)前多少分
? ? NSIntegercurMin = cmp.minute;
? ? //分針開始旋轉(zhuǎn)
? ? //計(jì)算分針當(dāng)前旋轉(zhuǎn)的角度
? ? //angle = 當(dāng)前多少分 * 每一分旋轉(zhuǎn)多少度
? ? CGFloatminA = curMin *perMinA;
? ? self.minL.transform = CATransform3DMakeRotation((minA), 0, 0, 1);
? ? //獲取當(dāng)前多少小時(shí)
? ? NSIntegercurHour = cmp.hour;
? ? //時(shí)針開始旋轉(zhuǎn)
? ? //計(jì)算時(shí)針當(dāng)前旋轉(zhuǎn)的角度
? ? //angle = 當(dāng)前多少小時(shí) * 每一分旋轉(zhuǎn)多少度
? ? 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{
? ? //所有旋轉(zhuǎn),縮放,都是繞著錨點(diǎn)進(jìn)行
? ? self.secL.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end