博客> 给小白白写的思路性动画时钟
给小白白写的思路性动画时钟
2017-08-16 01:22 评论:0 阅读:193 回眸一笑的羊蝎子
ios 头像 bitmap

仅仅是为了给小白白模拟思路用的代码, 各位大神请绕行~~~~

import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) CALayer *secondLayer;

@property (nonatomic,weak) CALayer *minuteLayer;

@property (nonatomic,assign) NSInteger minute;

@property (nonatomic,weak) CALayer *hourLayer;

@end

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;

    //表钟图片的设置 CALayer *clockLay = [CALayer layer];

    clockLay.contents = (__bridge id _Nullable)([UIImage imageNamed:@"clock"].CGImage); clockLay.position = self.view.center; clockLay.bounds = CGRectMake(0, 0, 150, 150);

    //切圆 clockLay.cornerRadius = 75; clockLay.masksToBounds = YES;

    [self.view.layer addSublayer:clockLay];

    //设置秒针 CALayer *secondLay = [CALayer layer];

    secondLay.backgroundColor = [UIColor redColor].CGColor; secondLay.bounds = CGRectMake(0, 0, 1, 75); secondLay.position = clockLay.position;

    //调整秒针的锚点 // secondLay.anchorPoint = CGPointMake(0,0.75); secondLay.anchorPoint = CGPointMake(0.5, 0.75);

    [self.view.layer addSublayer:secondLay];

    self.secondLayer = secondLay;

    //创建CADisplayLink CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveSecond)];

    //将创建的link添加到循环循环runLoop [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; // //这样取出来的当前时间会比系统的表慢一秒。 // NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(move) userInfo:nil repeats:YES];

pragma mark - 分针

CALayer *minuteLay = [CALayer layer];

minuteLay.backgroundColor = [UIColor blueColor].CGColor;
minuteLay.position = clockLay.position;
minuteLay.bounds = CGRectMake(0, 0, 1, 75);
minuteLay.anchorPoint = CGPointMake(0.5, 0.75);

[self.view.layer addSublayer:minuteLay];

self.minuteLayer = minuteLay;

CADisplayLink *linkMin = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveMin)];

[linkMin addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

pragma mark - 时针

CALayer *hourLay = [CALayer layer];

hourLay.backgroundColor = [UIColor orangeColor].CGColor;
hourLay.position = clockLay.position;
hourLay.bounds = CGRectMake(0, 0, 1, 75);
hourLay.anchorPoint = CGPointMake(0.5, 0.75);

[self.view.layer addSublayer:hourLay];

self.hourLayer = hourLay;

CADisplayLink *linkHour = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveHour)];

[linkHour addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

}

  • (void)moveHour{

    //时针 30度 CGFloat angle = (M_PI / 6);

    NSDate *date = [NSDate date];

    NSCalendar *calen = [NSCalendar currentCalendar];

    NSInteger currentHour = [calen component:NSCalendarUnitHour fromDate:date];

// NSLog(@"%f",self.minuteAngle);

// angle = angle currentHour + (self.minuteAngle / (M_PI)2 ) * angle;

// angle = angle * currentHour;

angle = currentHour * (30 / 180.0) * M_PI;

CGFloat angleMinToHour = self.minute * (0.5 / 180.0) * M_PI;

self.hourLayer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
self.hourLayer.transform = CATransform3DMakeRotation(angleMinToHour + angle, 0, 0, 1);

}

  • (void)moveMin{

    CGFloat angle = M_PI / 30;

    NSDate *date = [NSDate date];

    NSCalendar *calen = [NSCalendar currentCalendar];

    NSInteger currentMin = [calen component:NSCalendarUnitMinute fromDate:date];

// angle = angle * currentMin;

angle = currentMin * (6 / 180.0) * M_PI;

self.minute = currentMin;

self.minuteLayer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);

}

  • (void)moveSecond{

    //计算转动的角度。一秒6度 CGFloat angle = M_PI / 30;

    //当前的日期->当前的日历 NSDate *date = [NSDate date];

    NSCalendar *calen = [NSCalendar currentCalendar];

    //从当前日历中获取当前秒 //component 日历中所包含的unit 年月日时分秒等 NSInteger currentSecond = [calen component:NSCalendarUnitSecond fromDate:date];

    //开始时秒针所在的位置 // angle = angle currentSecond; angle = currentSecond (6 / 180.0) * M_PI;

    self.secondLayer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);

// 不能获取当前的时间进行转动 // //转动 围绕z轴转动 // self.secondLayer.transform = CATransform3DRotate(self.secondLayer.transform, angle, 0, 0, 1);

}

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

@end

收藏
0
sina weixin mail 回到顶部