#import "ViewController.h"
//获得当前的年月日 时分秒
#define CURRENTSEC [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]
#define CURRENTMIN [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]
#define CURRENTHOUR [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]
#define CURRENTDAY [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]
#define CURRENTMONTH [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]
#define CURRENTYEAR [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]
//角度转换成弧度
#define ANGEL(x) x/180.0 * M_PI
#define kPerSecondA ANGEL(6)
#define kPerMinuteA ANGEL(6)
#define kPerHourA ANGEL(30)
#define kPerHourMinuteA ANGEL(0.5)
@interface ViewController ()
@property (nonatomic,strong) UIImageView *imageClock;
@property (nonatomic,strong) CALayer *layerSec;
@property (nonatomic,strong) CALayer *layerMin;
@property (nonatomic,strong) CALayer *layerHour;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.imageClock];
[self.imageClock.layer addSublayer:self.layerSec];
[self.imageClock.layer addSublayer:self.layerMin];
[self.imageClock.layer addSublayer:self.layerHour];
[self timeChange];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)timeChange
{
self.layerSec.transform = CATransform3DMakeRotation(CURRENTSEC * kPerSecondA, 0, 0, 1);
self.layerMin.transform = CATransform3DMakeRotation(CURRENTMIN * kPerMinuteA, 0, 0, 1);
self.layerHour.transform = CATransform3DMakeRotation(CURRENTHOUR * kPerHourA, 0, 0, 1);
self.layerHour.transform = CATransform3DMakeRotation(CURRENTMIN * kPerHourMinuteA + CURRENTHOUR*kPerHourA, 0, 0, 1);
}
- (UIImageView *)imageClock
{
if (_imageClock == nil) {
_imageClock = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"钟表"]];
_imageClock.frame = CGRectMake(100, 100, 200, 200);
}
return _imageClock;
}
- (CALayer *)layerSec
{
if (_layerSec == nil) {
_layerSec = [CALayer layer];
_layerSec.bounds = CGRectMake(0, 0, 2, 80);
_layerSec.backgroundColor = [UIColor redColor].CGColor;
_layerSec.cornerRadius = 5;
_layerSec.anchorPoint = CGPointMake(0.5, 1);
_layerSec.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
}
return _layerSec;
}
- (CALayer *)layerMin
{
if (_layerMin == nil) {
_layerMin = [CALayer layer];
_layerMin.bounds = CGRectMake(0, 0, 4, 60);
_layerMin.backgroundColor = [UIColor blackColor].CGColor;
_layerMin.cornerRadius = 5;
_layerMin.anchorPoint = CGPointMake(0.5, 1);
_layerMin.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
}
return _layerMin;
}
- (CALayer *)layerHour
{
if (_layerHour == nil) {
_layerHour = [CALayer layer];
_layerHour.bounds = CGRectMake(0, 0, 6, 40);
_layerHour.backgroundColor = [UIColor blackColor].CGColor;
_layerHour.cornerRadius = 5;
_layerHour.anchorPoint = CGPointMake(0.5, 1);
_layerHour.position = CGPointMake(self.imageClock.bounds.size.width/2, self.imageClock.bounds.size.height/2);
}
return _layerHour;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/supersonico/article/details/46952221