iOS实现简易钟表-创新互联

本文实例为大家分享了iOS实现简易钟表的具体代码,供大家参考,具体内容如下

站在用户的角度思考问题,与客户深入沟通,找到叠彩网站设计与叠彩网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都网站设计、企业官网、英文网站、手机端网站、网站推广、国际域名空间、网页空间、企业邮箱。业务覆盖叠彩地区。

效果图:

iOS实现简易钟表

注意:表盘是一个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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:iOS实现简易钟表-创新互联
本文URL:http://pwwzsj.com/article/dpjegp.html