效果图


实现思路


  • 该示例通过隐式动画实现
  • 表盘通过显示在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);
    }
 
 

OC - 28.模拟时钟的更多相关文章

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

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

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

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

  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/ ...

随机推荐

  1. Bluetooth LE(低功耗蓝牙) - 第四部分

    回顾 在本系列前几篇文章中我们完成了BLE设备的发现 , 为我们的app通过BLE显示从TI SensorTag设备中获取到环境温度和湿度的工作打下了基础.在这篇文章中我们将着眼于连接到我们所发现的S ...

  2. bzoj1820

    水dp,状态表示三个司机当前在哪所用最小耗油,因为有一个一定在当前点所以可以压掉一维 ..,..] of longint;     a:..] of longint;     x,y,i,j,k,n, ...

  3. 浅谈Redis及其安装配置

    一.Redis的介绍 二.Redis的安装配置 三.Redis的配置文件说明 四.Redis的简单操作 简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型. ...

  4. activity跳转关闭软件盘

    之前试过 if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPE ...

  5. HDU 4727 The Number Off of FFF 2013年四川省赛题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4727 题目大意:队列里所有人进行报数,要找出报错的那个人 思路:,只要找出序列中与钱一个人的数字差不是 ...

  6. 选择排序(SelectSorted)

    //简单的选择排序public class SelectSorted { public static void main(String[] args) { int[] array={12,24,9,7 ...

  7. ACM1024动态规划

    #include <cstdio> #include <algorithm> #define FI(a, b, c) for(int a = (b); a <= (c); ...

  8. poj 1329 Circle Through Three Points(求圆心+输出)

    题目链接:http://poj.org/problem?id=1329 输出很蛋疼,要考虑系数为0,输出也不同 #include<cstdio> #include<cstring&g ...

  9. Conversion between json and object using SBJson lib

    Define two methods in an object class as follows: @interface MyObject : NSObject @property (nonatomi ...

  10. 323. Number of Connected Components in an Undirected Graph

    算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...