代码改变世界

手表(锚点)

2015-10-16 05:25  真实16  阅读(293)  评论(0编辑  收藏  举报

//

//  ViewController.m

//  UI-NO-37-1 模拟手表

//

//  Created by 容伟 on 15/9/16.

//  Copyright (c) 2015年 容伟. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

{

    CALayer *sLayer; // 秒针

    CALayer *mLayer; // 分针

    CALayer *hLayer; // 时针

    

    

 

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

//  表盘

    [self addWatchFace];

//  秒针

    [self pointLayer];

 

    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(fireTime)];

    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    

//    定时器 调用指针旋转

//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(fireTime) userInfo:nil repeats:YES];

 

}

 

 

//   添加表盘

- (void)addWatchFace {

    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"表盘" ofType:@"png"]];

    

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    imageView.frame = CGRectMake(0, 0, image.size.width/3, image.size.height/3);

    imageView.center = self.view.center;

    [self.view addSubview:imageView];

}

 

- (void)pointLayer {

//  秒针  分针  时针

    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"指针" ofType:@"png"]];

    sLayer = [[CALayer alloc] init];

    sLayer.bounds = CGRectMake(0, 0, image.size.width/8, image.size.height/4);

    sLayer.position = self.view.center;

    sLayer.anchorPoint = CGPointMake(0.5, 0.65);

    sLayer.contents = (__bridge id)(image.CGImage);

    [self.view.layer addSublayer:sLayer];

    

//  分针

    mLayer = [[CALayer alloc] init];

    mLayer.bounds = CGRectMake(0, 0, image.size.width/7, image.size.height/5);

    mLayer.position = self.view.center;

    mLayer.anchorPoint = CGPointMake(0.5, 0.65);

    mLayer.contents = (id)image.CGImage;

    [self.view.layer addSublayer:mLayer];

 

//  时针

    hLayer = [[CALayer alloc] init];

    hLayer.bounds = CGRectMake(0, 0, image.size.width/6, image.size.height/7);

    hLayer.position = self.view.center;

    hLayer.anchorPoint = CGPointMake(0.5, 0.65);

    hLayer.contents = (id)image.CGImage;

    [self.view.layer addSublayer:hLayer];

 

 

}

 

// 定时器 调用方法

- (void)fireTime {

    NSCalendar *calendar = [NSCalendar currentCalendar];

    

    //    获得当前的时间 小时

    //    component:fromDate: 从日历里面的某个日期 获得某个组件

    NSInteger h =[calendar component:NSCalendarUnitHour fromDate:[NSDate date]];

//    NSLog(@"%ld", (long)h);

    

    //    获得当前时间 分

    NSInteger m =[calendar component:NSCalendarUnitMinute fromDate:[NSDate date]];

    

    //    获得当前时间 秒

    NSInteger s =[calendar component:NSCalendarUnitSecond fromDate:[NSDate date]];

    sLayer.transform = CATransform3DMakeRotation(M_PI/30*s, 0, 0, 1);

    

#warning 有错

    mLayer.transform = CATransform3DMakeRotation(M_PI/30*m + (M_PI/(30*60))*s, 0, 0, 1);

    hLayer.transform = CATransform3DMakeRotation(M_PI/6*h + (M_PI/(6*60))*m, 0, 0, 1);

}

 

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end