#import "WatchViewController.h"
//钟表图片的高
#define kWatchH self.watchImage.bounds.size.height
//钟表图片的宽
#define kWatchW self.watchImage.bounds.size.width
//秒针每秒转多少度
#define angle_second 6
//分针每分钟转多少度
#define angle_minute 6
//时针每小时转多少度
#define angle_hour 30
//时针每分钟转多少度
#define anglem_minute_hour 0.5
@interface WatchViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *watchImage;
@property (nonatomic ,retain) NSTimer *timer;
@property (nonatomic ,retain) CALayer *secondL;
@property (nonatomic ,retain) CALayer *minuteL;
@property (nonatomic ,retain) CALayer *hourL;
@end
@implementation WatchViewController
- (void)viewDidDisappear:(BOOL)animated
{
[_timer invalidate];
_timer = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUpHourLayer];
[self setUpMinuteLayer];
[self setUpSecondLayer];
[self timeChange];
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
- (void)timeChange
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
CGFloat com_second = comp.second;
CGFloat com_minute = comp.minute;
CGFloat com_hour = comp.hour;
CGFloat secondA = (angle_second * com_second)/180 *M_PI;
CGFloat minuteA = (angle_minute * com_minute)/180 *M_PI;
CGFloat hourA = ((angle_hour * com_hour)+com_minute*anglem_minute_hour)/180 *M_PI;
_secondL.transform = CATransform3DMakeRotation(secondA, 0, 0, 1);
_minuteL.transform = CATransform3DMakeRotation(minuteA, 0, 0, 1);
_hourL.transform = CATransform3DMakeRotation(hourA, 0, 0, 1);
}
#pragma mark 生成时针
- (void)setUpHourLayer
{
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor blackColor].CGColor;
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(kWatchW * 0.5,kWatchH * 0.5 );
layer.bounds = CGRectMake(0, 0, 4, kWatchH * 0.5 - 30);
_hourL = layer;
[_watchImage.layer addSublayer:_hourL];
}
#pragma mark 生成分针
- (void)setUpMinuteLayer
{
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor blackColor].CGColor;
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(kWatchW * 0.5,kWatchH * 0.5 );
layer.bounds = CGRectMake(0, 0, 4, kWatchH * 0.5);
_minuteL = layer;
[_watchImage.layer addSublayer:_minuteL];
}
#pragma mark 生成秒针
- (void)setUpSecondLayer
{
CALayer *layer = [CALayer layer];
layer.backgroundColor = [UIColor redColor].CGColor;
layer.anchorPoint = CGPointMake(0.5, 1);
layer.position = CGPointMake(kWatchW * 0.5,kWatchH * 0.5 );
layer.bounds = CGRectMake(0, 0, 1, kWatchH * 0.5);
_secondL = layer;
[_watchImage.layer addSublayer:_secondL];
}
网友评论