亲宝软件园·资讯

展开

iOS钟表 iOS实现简易钟表

LayneCheung 人气:0

效果图:

注意:表盘是一个UIImageView控件,设置image为表盘图片

核心代码:

//
// ViewController.m
// 时钟
//
// Created by llkj on 2017/8/29.
// Copyright © 2017年 LayneCheung. All rights reserved.
//

#import "ViewController.h"

//每一秒旋转多少度
#define perSecA 6
//每一分旋转多少度
#define perMinA 6
//每一小时旋转多少度
#define perHourA 30

//每一分时针旋转的度数
#define perMinHour 0.5
//角度转弧度
#define angle2Rad(angle) ((angle) / 180.0 * M_PI)
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *clockView;
@property (nonatomic, weak) CALayer *secL;
@property (nonatomic, weak) CALayer *minL;
@property (nonatomic, weak) CALayer *hourL;
@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 [self setHour];
 [self setMin];
 [self setSec];

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

- (void)timeChange{

 //获取当前秒
 NSCalendar *cal = [NSCalendar currentCalendar];
 NSDateComponents *cmp = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
 NSInteger curSec = cmp.second + 1;
 NSInteger curMin = cmp.minute;
 NSInteger curHour = cmp.hour;

 //秒针开始旋转
 //计算秒针当前旋转的角度
 // angle = 当前多少秒 * 每一秒旋转多少度
 CGFloat secA = curSec * perSecA;
 //旋转方向是Z轴
 self.secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1);


 //分针开始旋转
 //计算分针当前旋转的角度
 // angle = 当前多少分 * 每一分旋转多少度
 CGFloat minA = curMin * perMinA;
 self.minL.transform = CATransform3DMakeRotation(angle2Rad(minA), 0, 0, 1);


 //时针开始旋转
 //计算时针当前旋转的角度
 // angle = 当前多少时 * 每一小时旋转多少度
 CGFloat hourA = curHour * perHourA + curMin * perMinHour;

 self.hourL.transform = CATransform3DMakeRotation(angle2Rad(hourA), 0, 0, 1);
}
//添加秒针
- (void)setSec{

 CALayer *secL = [CALayer layer];
 secL.bounds = CGRectMake(0, 0, 1, 80);
 secL.backgroundColor = [UIColor redColor].CGColor;
 //绕着锚点旋转
 secL.anchorPoint = CGPointMake(0.5, 1);
 secL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
 [self.clockView.layer addSublayer:secL];
 self.secL = secL;

}

//添加分针
- (void)setMin{

 CALayer *minL = [CALayer layer];
 minL.bounds = CGRectMake(0, 0, 3, 70);
 minL.cornerRadius = 1.5;
 minL.backgroundColor = [UIColor blackColor].CGColor;
 minL.anchorPoint = CGPointMake(0.5, 1);
 minL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
 [self.clockView.layer addSublayer:minL];
 self.minL = minL;

}

//添加时针
- (void)setHour{

 CALayer *hourL = [CALayer layer];
 hourL.bounds = CGRectMake(0, 0, 3, 60);
 hourL.backgroundColor = [UIColor blackColor].CGColor;
 hourL.anchorPoint = CGPointMake(0.5, 1);
 hourL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
 [self.clockView.layer addSublayer:hourL];
 self.hourL = hourL;

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


@end

加载全部内容

相关教程
猜你喜欢
用户评论