#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];
}
钟表
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...