效果图


实现思路


  • 该示例通过隐式动画实现
  • 表盘通过显示在imageView中的一张图片来实现
  • 在表盘上绘制(时分秒)三条直线,分别位于不同的图层,且时针位于最下层,秒针位于最上层
    • 设置直线为圆角
    • 直线的一段位于表盘的圆心
    • 通过NSTimer定时器,使不同的图层定时执行对应的动画

实现步骤


  • 通过storyboard创建表盘,并拥有它

    @property (weak, nonatomic) IBOutlet UIImageView *clockView;
  • 使用三个成员变量来保存时分秒三根表针位于的不同图层

    @property (nonatomic, weak) CALayer *secondLayer;
    @property (nonatomic, weak) CALayer *minuteLayer;
    @property (nonatomic, weak) CALayer *hourLayer;
  • 初始化所用到的常量

    //将旋转角度转换为弧度制
    #define angleToRadion(angle) ((angle) / 180.0 * M_PI) //秒针每秒钟转过的角度
    #define perSecondAngle 6
    //分针每分钟转过的角度
    #define perMinuteAngle 6
    //时针每小时转过的角度
    #define perHourAngle 30
    //时针每分钟转过的角度
    #define perMuniteHourAngle 0.5
  • 设置时分秒三根表针

    • 设置时针

      - (void)setUpHourLayer
      {
      //创建图层
      CALayer *layer = [CALayer layer];
      layer.backgroundColor = [UIColor blackColor].CGColor;
      layer.cornerRadius = 8; 设置图层的锚点
      layer.anchorPoint = CGPointMake(0.5, 1);
      //设置图层的位置和尺寸
      layer.position = CGPointMake(kClockWith * 0.5, kClockWith * 0.5);
      layer.bounds = CGRectMake(0, 0, 5, kClockWith * 0.5 - 44); //将图层添加到父图层中
      [self.clockView.layer addSublayer:layer];
      self.hourLayer = layer;
      }
    • 设置分针

      - (void)setUpMinuteLayer
      {
      CALayer *layer = [CALayer layer];
      layer.backgroundColor = [UIColor blackColor].CGColor;
      layer.cornerRadius = 4; //设置锚点
      layer.anchorPoint = CGPointMake(0.5, 1); //设置位置和尺寸
      layer.position = CGPointMake(kClockWith * 0.5, kClockWith * 0.5);
      layer.bounds = CGRectMake(0, 0, 3, kClockWith * 0.5 - 34); //将图层添加到父图层中
      [self.clockView.layer addSublayer:layer];
      self.minuteLayer = layer;
      }
    • 设置秒针

      - (void)setUpSecondLayer
      {
      CALayer *layer = [CALayer layer];
      layer.backgroundColor = [UIColor redColor].CGColor; //设置锚点
      layer.anchorPoint = CGPointMake(0.5, 1); //设置位置和尺寸
      layer.position = CGPointMake(kClockWith * 0.5, kClockWith * 0.5);
      layer.bounds = CGRectMake(0, 0, 1, kClockWith * 0.5 - 24); //将图层添加到父图层中
      [self.clockView.layer addSublayer:layer];
      self.secondLayer = layer;
      }
  • 设置定时器,定时执行动画

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
  • 设置定时器触发时调用的方式,添加动画代码

    - (void)timeChange
    {
    //获取日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar]; //获取日期组件
    NSDateComponents *components = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; //获取当前的时分秒值
    NSInteger second = components.second;
    NSInteger munite = components.minute;
    NSInteger hour = components.hour; //计算当前时分秒表针转过的角度(弧度制)
    CGFloat secondAngle = angleToRadion(second * perSecondAngle);
    CGFloat muniteAngle = angleToRadion(munite * perMinuteAngle);
    CGFloat hourAngle = angleToRadion(hour *perHourAngle + munite * perMuniteHourAngle); //修改时分秒表针位于的图层的transform属性,执行隐式动画
    self.secondLayer.transform = CATransform3DMakeRotation(secondAngle, 0, 0, 1);
    self.minuteLayer.transform = CATransform3DMakeRotation(muniteAngle, 0, 0, 1);
    self.hourLayer.transform = CATransform3DMakeRotation(hourAngle, 0, 0, 1);
    }

CoreAnimation-09-模拟时钟的更多相关文章

  1. 一个模拟时钟的时间选择器 ClockPicker

    最近开发的一个模拟时钟的时间选择器 ClockPicker,用于 Bootstrap,或者单独作为一个 jQuery 插件. 源代码托管在 GitHub 上: ClockPicker 最近项目中需要用 ...

  2. 模拟时钟(AnalogClock)和数字时钟(DigitalClock)

    Demo2\clock_demo\src\main\res\layout\activity_main.xml <LinearLayout xmlns:android="http://s ...

  3. android脚步---数字时钟和模拟时钟

    时钟UI组件是两个非常简单的组件,分为Digitalclock  和Analogclock, main.xml文件,书中程序有问题,加了两个组件,一个Button和一个<Chronometer ...

  4. Java多线程之sleep方法阻塞线程-模拟时钟

    package org.study2.javabase.ThreadsDemo.status; import java.text.SimpleDateFormat; import java.util. ...

  5. css模拟时钟

    css模拟时钟 思路: 画时钟数字(x,y)坐标 x = x0 + r*cos(deg) y = y0 + r*sin(deg) 知识点: 创建元素: createElement 添加元素: appe ...

  6. 【CSS3】纯CSS代码实现模拟时钟,+js对时功能。

    使用CSS3纯代码来实现模拟时钟,及指针动画功能. 在这里主要使用到css3一些基本元素: border-radius:圆角边框,画圆形:表盘 Transform:变换,旋转,扭曲:刻度盘,指针形状 ...

  7. 模拟时钟(AnalogClock)

    模拟时钟(AnalogClock) 显示一个带时钟和分针的表面 会随着时间的推移变化 常用属性: android:dial 可以为表面提供一个自定义的图片 下面我们直接看代码: 1.Activity ...

  8. Windows下编程--模拟时钟的实现

    windows下编程--模拟时钟的实现: 主要可以分为几个步骤: (1)   编写按键事件处理(启动和停止时钟) (2)   编写时钟事件处理,调用显示时钟函数 (3)   编写显示时钟函数,要调用显 ...

  9. 变换CALayer锚点实现模拟时钟的动画

    变换CALayer锚点实现模拟时钟的动画 变换锚点得需要一点理论知识,看下图就能明白:). https://developer.apple.com/library/ios/documentation/ ...

  10. VC++SDK编程——模拟时钟

    #include <Windows.h> #include <tchar.h> #include <math.h> typedef struct Time { in ...

随机推荐

  1. 编写高质量JS代码的68个有效方法(十二)

    No.56.避免不必要的状态 Tips: 尽可能地使用无状态的API 如果API是有状态的,标示出每个操作与哪些状态有关联 无状态的API简洁,更容易学习和使用,也不需要考虑其他的状态.如: 'tes ...

  2. Direct2D开发:从资源加载位图

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D使用Windows图像处理组件 (WIC) 来加载位图.从文件加载位图的方法很简单,而且网上的教 ...

  3. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  4. UnityShader快速上手指南(二)

    简介 前一篇介绍了如果编写最基本的shader,接下来本文将会简单的深入一下,我们先来看下效果吧 呃,gif效果不好,实际效果是很平滑的动态过渡 实现思路 1.首先我们要实现一个彩色方块 2.让色彩动 ...

  5. 剑指offer面试题31连续子数组的最大和

    一.题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果 ...

  6. Mongodb 语法,update,insert,delete,find

    ---恢复内容开始--- db.Users.update({OrganizationCode:"Global"},{$set:{OrganizationCode:"Fre ...

  7. 【Bootstrap基础学习】03 Bootstrap插件示例

    模态框 <h2>创建模态框(Modal)</h2> <!-- 按钮触发模态框 --> <button class="btn btn-primary ...

  8. Ajax,谷歌提示AutoCompleteExtender控件

    提示内容从数据库中读取: ------------------------------------------页面 <asp:ScriptManager ID="ScriptManag ...

  9. How to create water Ripple effect using HTML5 canvas

    https://www.script-tutorials.com/how-to-create-water-drops-effect-using-html5-canvas/ https://www.sc ...

  10. Java、Hibernate(JPA)注解大全

    1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...