****

  1. #import "HMViewController.h"
  2.  
  3. // 每秒秒针转6度
  4. #define perSecendA 6
  5.  
  6. // 每分钟分针转6度
  7. #define perMinuteA 6
  8.  
  9. // 每小时时针转6度
  10. #define perHourA 30
  11.  
  12. // 每分钟时针转6度
  13. #define perMinuteHourA 0.5
  14.  
  15. #define angle2radian(x) ((x) / 180.0 * M_PI)
  16.  
  17. @interface HMViewController ()
  18. {
  19. CALayer *_second;
  20. CALayer *_minute;
  21. CALayer *_hour;
  22. }
  23. @property (weak, nonatomic) IBOutlet UIView *redView;
  24. @property (weak, nonatomic) IBOutlet UIImageView *clockView;
  25.  
  26. @end
  27.  
  28. @implementation HMViewController
  29.  
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view, typically from a nib.
  34. // 1.添加秒针
  35. [self addSecond];
  36.  
  37. // 2.添加分针
  38. [self addMintue];
  39.  
  40. // 3.添加时针
  41. [self addHour];
  42.  
  43. // 创建定时器
  44. [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(update) userInfo:nil repeats:YES];
  45.  
  46. [self update];
  47. }
  48.  
  49. - (void)addHour
  50. {
  51. CGFloat imageW = _clockView.bounds.size.width;
  52. CGFloat imageH = _clockView.bounds.size.height;
  53.  
  54. // 1.添加时针
  55. CALayer *hour = [CALayer layer];
  56.  
  57. // 2.设置锚点
  58. hour.anchorPoint = CGPointMake(0.5, );
  59.  
  60. // 3.设置位置
  61. hour.position = CGPointMake(imageW * 0.5, imageH * 0.5);
  62.  
  63. // 4.设置尺寸
  64. hour.bounds = CGRectMake(, , , imageH * 0.5 - );
  65.  
  66. // 5.红色
  67. hour.backgroundColor = [UIColor blackColor].CGColor;
  68.  
  69. hour.cornerRadius = ;
  70.  
  71. // 添加到图层上
  72. [_clockView.layer addSublayer:hour];
  73.  
  74. _hour = hour;
  75. }
  76.  
  77. // 添加分针
  78. - (void)addMintue
  79. {
  80. CGFloat imageW = _clockView.bounds.size.width;
  81. CGFloat imageH = _clockView.bounds.size.height;
  82.  
  83. // 1.添加分针
  84. CALayer *minute = [CALayer layer];
  85.  
  86. // 2.设置锚点
  87. minute.anchorPoint = CGPointMake(0.5, );
  88.  
  89. // 3.设置位置
  90. minute.position = CGPointMake(imageW * 0.5, imageH * 0.5);
  91.  
  92. // 4.设置尺寸
  93. minute.bounds = CGRectMake(, , , imageH * 0.5 - );
  94.  
  95. // 5.红色
  96. minute.backgroundColor = [UIColor blueColor].CGColor;
  97.  
  98. // 添加到图层上
  99. [_clockView.layer addSublayer:minute];
  100.  
  101. _minute = minute;
  102. }
  103.  
  104. // 添加秒针
  105. - (void)addSecond
  106. {
  107. CGFloat imageW = _clockView.bounds.size.width;
  108. CGFloat imageH = _clockView.bounds.size.height;
  109.  
  110. // 1.添加秒针
  111. CALayer *second = [CALayer layer];
  112.  
  113. // 2.设置锚点
  114. second.anchorPoint = CGPointMake(0.5, );
  115.  
  116. // 3.设置位置
  117. second.position = CGPointMake(imageW * 0.5, imageH * 0.5);
  118.  
  119. // 4.设置尺寸
  120. second.bounds = CGRectMake(, , , imageH * 0.5 - );
  121.  
  122. // 5.红色
  123. second.backgroundColor = [UIColor redColor].CGColor;
  124.  
  125. // 添加到图层上
  126. [_clockView.layer addSublayer:second];
  127.  
  128. _second = second;
  129. }
  130.  
  131. - (void)update
  132. {
  133. // 获取秒数
  134. // 获取日历对象
  135. NSCalendar *calendar = [NSCalendar currentCalendar];
  136.  
  137. // 获取日期组件
  138. NSDateComponents *compoents = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
  139.  
  140. // 获取秒数
  141. CGFloat sec = compoents.second;
  142.  
  143. // 获取分钟
  144. CGFloat minute = compoents.minute;
  145.  
  146. // 获取小时
  147. CGFloat hour = compoents.hour;
  148.  
  149. // 算出秒针旋转多少°
  150. CGFloat secondA = sec * perSecendA;
  151.  
  152. // 算出分针旋转多少°
  153. CGFloat minuteA = minute * perMinuteA;
  154.  
  155. // 算出时针旋转多少°
  156. CGFloat hourA = hour * perHourA;
  157. hourA += minute * perMinuteHourA;
  158.  
  159. // 旋转秒针
  160. _second.transform = CATransform3DMakeRotation(angle2radian(secondA), , , );
  161.  
  162. // 旋转分针
  163. _minute.transform = CATransform3DMakeRotation(angle2radian(minuteA), , , );
  164.  
  165. // 旋转时针
  166. _hour.transform = CATransform3DMakeRotation(angle2radian(hourA), , , );
  167.  
  168. }
  169. @end

IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)的更多相关文章

  1. IOS第18天(9,核心动画-动画组)

    ****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...

  2. IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)

    ****动画效果 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDurat ...

  3. IOS第18天(10,核心动画-转盘,自定义buton,旋转动画)

    *****HMViewController.m #import "HMViewController.h" #import "HMWheelView.h" @in ...

  4. IOS第18天(8,核心动画转场动画)

    ***翻页效果 #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...

  5. [iOS UI进阶 - 6.1] 核心动画CoreAnimation

    A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...

  6. [iOS UI进阶 - 6.2] 核心动画CoreAnimation 练习代码

    A.基本用法 1.CABasicAnimation // // ViewController.m // CoreAnimationTest // // Created by hellovoidworl ...

  7. iOS UI进阶-3.0 核心动画

    Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<Quar ...

  8. ios开发图层layer与核心动画二:CATransform3D,CAlayear和UIView区别,layer的position和anchorpoint

    一:CATransform3D #import "ViewController.h" @interface ViewController () @property (weak, n ...

  9. ios开发之图层与核心动画一:图层CALayer的认识

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

随机推荐

  1. Linux IO模型和网络编程模型

    术语概念描述: IO有内存IO.网络IO和磁盘IO三种,通常我们说的IO指的是后两者. 阻塞和非阻塞,是函数/方法的实现方式,即在数据就绪之前是立刻返回还是等待. 以文件IO为例,一个IO读过程是文件 ...

  2. 模数转换(A/D)和数模转换(D/A) 图示

    (从书中截图) 在时间域和频率域中示意图: 1.A/D转换 2.D/A转换

  3. 【gulp-sass】error: File to import not found or unreadable

    简要记录一下在使用gulp-sass时候踩的坑,虽然不明所以然,但是似乎在https://github.com/dlmanning/gulp-sass/issues/1 找到了答案. 在使用gulpf ...

  4. Type.GetField 修改类中私有字段。

    上一篇Popup Bug中修改了SystemParameters类中静态只读属性MenuDropAlignment. var t = typeof(SystemParameters); var fie ...

  5. MFC MessageBox AfxMessageBox

    MessageBox 一.消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合.1.MessageBox("这是一个最简单的消息框!");2.M ...

  6. Open CV 播放视频(2)

      演示:读取一个视频,然后播放,ESC退出.   #include "stdafx.h"   #include <opencv2/core/core.hpp>   # ...

  7. BZOJ4428 : [Nwerc2015]Debugging调试

    设$f[i]$为最优策略下调试$i$行代码的时间,则: $f[1]=0$ $f[i]=\min((j-1)\times p+f[\lceil\frac{i}{j}\rceil])+r$ 意义为枚举pr ...

  8. 【BZOJ1984】月下“毛景树” 树链剖分+线段树

    [BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...

  9. Activiti工作流学习(二)流程实例、执行对象、任务

    一.前言 前面说明了基本的流程部署.定义,启动流程实例等基本操作,下面我们继续来学习流程实例.执行对象.任务. 二.流程实例.执行对象说明 整个Activiti的生命周期经过了如下的几个步骤: 1.流 ...

  10. Universal JS module loader

    With dependency ;(function (root, factory) { if (typeof define === 'function' && define.amd) ...