用代码实现时钟功能
2015-11-27 23:02
375 查看
// 每一秒旋转的角度
#define perSec 6
// 每一分钟旋转的角度
#define perMin 6
// 每一个小时旋转的角度
#define perHour 30
// 每一分钟所对应时针旋转的角度
#define perMin2hourAngle 0.5
#import "ViewController.h"
@interface
ViewController ()
@property (weak,
nonatomic) IBOutlet
UIImageView *myView;
@property(nonatomic,
strong)CALayer *secLayer;
@property(nonatomic,
strong)CALayer *hourLayer;
@property(nonatomic,
strong)CALayer *minLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self setUpSec];
[self setUpMin];
[self
setUpHour];
// 创建一个定时器
[NSTimer
scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timeChange)
userInfo:nil
repeats:YES];
[self
timeChange];
}
- (void)timeChange
{
// 获取当前日历
NSCalendar *calendar = [NSCalendar
currentCalendar];
// 获取当前日期单元
NSDateComponents *cmp = [calendar
components:NSCalendarUnitHour|
NSCalendarUnitSecond|NSCalendarUnitMinute
fromDate:[NSDate
date]];
// 获取当前日期中的当前秒数
NSInteger sec = cmp.second;
// 获取当前日期中的分钟数
NSInteger min = cmp.minute;
// 获取当前日期中的小时数
NSInteger hour = cmp.hour;
// 计算秒针走过的弧度
CGFloat secAngle =
perSec * sec /180.0 *M_PI;
// 计算分针走过的弧度
CGFloat minAngle =
perMin * min /180.0 *M_PI;
// 计算时针走过的弧度
CGFloat hourAngle = (perHour * hour+
perMin2hourAngle * min) /180.0 *M_PI ;
self.secLayer.transform =
CATransform3DMakeRotation(secAngle,
0, 0, 1);
self.minLayer.transform =
CATransform3DMakeRotation(minAngle,
0, 0, 1);
self.hourLayer.transform =
CATransform3DMakeRotation(hourAngle,
0, 0, 1);
}
- (void)setUpSec
{
// 创建一个非根图层(秒针)
CALayer *seclayer = [CALayer
layer];
self.secLayer = seclayer;
seclayer.backgroundColor = [UIColor
redColor].CGColor;
seclayer.bounds =
CGRectMake(0,
0, 2, 80);
seclayer.anchorPoint =
CGPointMake(0.5,
1);
seclayer.position =
CGPointMake(self.myView.bounds.size.width
*0.5,
self.myView.bounds.size.height
*0.5);
//
把图层添加到根图层中
[self.myView.layer
addSublayer:seclayer];
}
- (void)setUpMin
{
// 创建一个非根图层(秒针)
CALayer *minlayer = [CALayer
layer];
self.minLayer = minlayer;
minlayer.backgroundColor = [UIColor
blackColor].CGColor;
minlayer.bounds =
CGRectMake(0,
0, 3, 70);
minlayer.anchorPoint =
CGPointMake(0.5,
1);
minlayer.position =
CGPointMake(self.myView.bounds.size.width
*0.5,
self.myView.bounds.size.height
*0.5);
//
设置时针的圆角半径
minlayer.cornerRadius =
1;
//
把图层添加到根图层中
[self.myView.layer
addSublayer:minlayer];
}
- (void)setUpHour
{
// 创建一个非根图层(秒针)
CALayer *hourlayer = [CALayer
layer];
self.hourLayer = hourlayer;
hourlayer.backgroundColor = [UIColor
blackColor].CGColor;
hourlayer.bounds =
CGRectMake(0,
0, 4, 60);
hourlayer.anchorPoint =
CGPointMake(0.5,
1);
hourlayer.position =
CGPointMake(self.myView.bounds.size.width
*0.5,
self.myView.bounds.size.height
*0.5);
//
设置时针的圆角半径
hourlayer.cornerRadius =
2;
//
把图层添加到根图层中
[self.myView.layer
addSublayer:hourlayer];
}
@end
#define perSec 6
// 每一分钟旋转的角度
#define perMin 6
// 每一个小时旋转的角度
#define perHour 30
// 每一分钟所对应时针旋转的角度
#define perMin2hourAngle 0.5
#import "ViewController.h"
@interface
ViewController ()
@property (weak,
nonatomic) IBOutlet
UIImageView *myView;
@property(nonatomic,
strong)CALayer *secLayer;
@property(nonatomic,
strong)CALayer *hourLayer;
@property(nonatomic,
strong)CALayer *minLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self setUpSec];
[self setUpMin];
[self
setUpHour];
// 创建一个定时器
[NSTimer
scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timeChange)
userInfo:nil
repeats:YES];
[self
timeChange];
}
- (void)timeChange
{
// 获取当前日历
NSCalendar *calendar = [NSCalendar
currentCalendar];
// 获取当前日期单元
NSDateComponents *cmp = [calendar
components:NSCalendarUnitHour|
NSCalendarUnitSecond|NSCalendarUnitMinute
fromDate:[NSDate
date]];
// 获取当前日期中的当前秒数
NSInteger sec = cmp.second;
// 获取当前日期中的分钟数
NSInteger min = cmp.minute;
// 获取当前日期中的小时数
NSInteger hour = cmp.hour;
// 计算秒针走过的弧度
CGFloat secAngle =
perSec * sec /180.0 *M_PI;
// 计算分针走过的弧度
CGFloat minAngle =
perMin * min /180.0 *M_PI;
// 计算时针走过的弧度
CGFloat hourAngle = (perHour * hour+
perMin2hourAngle * min) /180.0 *M_PI ;
self.secLayer.transform =
CATransform3DMakeRotation(secAngle,
0, 0, 1);
self.minLayer.transform =
CATransform3DMakeRotation(minAngle,
0, 0, 1);
self.hourLayer.transform =
CATransform3DMakeRotation(hourAngle,
0, 0, 1);
}
- (void)setUpSec
{
// 创建一个非根图层(秒针)
CALayer *seclayer = [CALayer
layer];
self.secLayer = seclayer;
seclayer.backgroundColor = [UIColor
redColor].CGColor;
seclayer.bounds =
CGRectMake(0,
0, 2, 80);
seclayer.anchorPoint =
CGPointMake(0.5,
1);
seclayer.position =
CGPointMake(self.myView.bounds.size.width
*0.5,
self.myView.bounds.size.height
*0.5);
//
把图层添加到根图层中
[self.myView.layer
addSublayer:seclayer];
}
- (void)setUpMin
{
// 创建一个非根图层(秒针)
CALayer *minlayer = [CALayer
layer];
self.minLayer = minlayer;
minlayer.backgroundColor = [UIColor
blackColor].CGColor;
minlayer.bounds =
CGRectMake(0,
0, 3, 70);
minlayer.anchorPoint =
CGPointMake(0.5,
1);
minlayer.position =
CGPointMake(self.myView.bounds.size.width
*0.5,
self.myView.bounds.size.height
*0.5);
//
设置时针的圆角半径
minlayer.cornerRadius =
1;
//
把图层添加到根图层中
[self.myView.layer
addSublayer:minlayer];
}
- (void)setUpHour
{
// 创建一个非根图层(秒针)
CALayer *hourlayer = [CALayer
layer];
self.hourLayer = hourlayer;
hourlayer.backgroundColor = [UIColor
blackColor].CGColor;
hourlayer.bounds =
CGRectMake(0,
0, 4, 60);
hourlayer.anchorPoint =
CGPointMake(0.5,
1);
hourlayer.position =
CGPointMake(self.myView.bounds.size.width
*0.5,
self.myView.bounds.size.height
*0.5);
//
设置时针的圆角半径
hourlayer.cornerRadius =
2;
//
把图层添加到根图层中
[self.myView.layer
addSublayer:hourlayer];
}
@end
相关文章推荐
- java基础学习总结——流
- PHP实现验证码功能
- conv2、filter2、imfilter的区别 及 conv2 matlab 代码
- yii2自定义form表单
- spring这么流行的原因是什么
- PHP注册登录:包括数据存入数据库,生成随机验证码
- Hello World基本代码以及Java基础知识
- myeclipse插件的安装
- Java8的新特性
- java设计模式---工厂方法和抽象工厂模式
- Struts2 type="chain"的用法详细解释
- 【C语言】-一维数组
- 2015.11.27---Java
- 字符串在内存中的存储
- c++11baohan线程安全的队列
- windows下php5.4安装imagick
- SpringMVC访问静态资源的三种方式
- java线程同步的5种方式(转载)
- JAVA8永久代
- leetcode笔记:Combination Sum III