如何动态绘制时钟

简介:

如何动态绘制时钟

 

效果

 

源码

https://github.com/YouXianMing/Animations


//
//  ClockViewController.m
//  Animations
//
//  Created by YouXianMing on 15/12/3.
//  Copyright © 2015年 YouXianMing. All rights reserved.
//

#import "ClockViewController.h"
#import "GCD.h"
#import "UIView+SetRect.h"
#import "SystemTimeInfomation.h"
#import "RotateAnimationView.h"
#import "UIView+GlowView.h"
#import "EasingRotateAnimationType.h"
#import "POPSpringRotateAnimationType.h"

#define   ONE_SEC   (M_PI * 2 / 60.f)
#define   ONE_MIN   (M_PI * 2 / 3600.f)
#define   ONE_HOUR  (M_PI * 2 / 3600.f / 12.f)

@interface ClockViewController ()

@property (nonatomic, strong)  RotateAnimationView  *hourView;
@property (nonatomic, strong)  RotateAnimationView  *secondView;
@property (nonatomic, strong)  RotateAnimationView  *minuteView;

@property (nonatomic)          CGFloat               hourCount;
@property (nonatomic)          CGFloat               secondCount;
@property (nonatomic)          CGFloat               minuteCount;

@property (nonatomic, strong)  GCDTimer             *timer;

@end

@implementation ClockViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
}

- (void)setup {

    [super setup];
    
    NSDictionary *currentTime = [[SystemTimeInfomation sharedInstance] currentTimeInfomation];
    
    CGFloat min = [currentTime[@"mm"] floatValue];
    CGFloat sec = [currentTime[@"ss"] floatValue];
    CGFloat hour = [currentTime[@"HH"] floatValue];
    
    {
        // 小时
        _hourCount                     = ONE_HOUR * (60 * 60 * hour + min * 60 + sec);
        self.hourView                  = [[RotateAnimationView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        self.hourView.center           = self.view.center;
        self.hourView.duration         = 0.75f;
        self.hourView.fromCircleRadian = 0.f;
        self.hourView.toCircleRadian   = self.hourView.fromCircleRadian + _hourCount;
        [self.view addSubview:self.hourView];
        [self.hourView startRotateAnimated:NO];
        
        // 小时图片
        UIImageView *hourView          = [[UIImageView alloc] initWithFrame:self.hourView.bounds];
        hourView.image                 = [UIImage imageNamed:@"hour"];
        [self.hourView addSubview:hourView];
        
        hourView.glowRadius            = @(2.f);
        hourView.glowOpacity           = @(0.75f);
        hourView.glowColor             = [[UIColor redColor] colorWithAlphaComponent:1.f];
        
        hourView.glowDuration          = @(1.f);
        hourView.hideDuration          = @(1.f);
        hourView.glowAnimationDuration = @(1.f);
        
        [hourView createGlowLayer];
        [hourView insertGlowLayer];
        [hourView startGlowLoop];
    }
    
    {
        // 分钟
        _minuteCount                     = ONE_MIN * (min * 60 + sec);
        self.minuteView                  = [[RotateAnimationView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        self.minuteView.center           = self.view.center;
        self.minuteView.duration         = 0.75f;
        self.minuteView.fromCircleRadian = 0.f;
        self.minuteView.toCircleRadian   = self.minuteView.fromCircleRadian + _minuteCount;
        [self.view addSubview:self.minuteView];
        [self.minuteView startRotateAnimated:NO];
        
        // 分钟图片
        UIImageView *minuteView          = [[UIImageView alloc] initWithFrame:self.minuteView.bounds];
        minuteView.image                 = [UIImage imageNamed:@"min"];
        [self.minuteView addSubview:minuteView];
    }
    
    {
        // 秒钟
        _secondCount                     = ONE_SEC * sec;
        self.secondView                  = [[RotateAnimationView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        self.secondView.center           = self.view.center;
        self.secondView.duration         = 0.75f;
        self.secondView.fromCircleRadian = 0.f;
        self.secondView.toCircleRadian   = self.secondView.fromCircleRadian + _secondCount;
        self.secondView.animationType    = [EasingRotateAnimationType new];
//        self.secondView.animationType    = [POPSpringRotateAnimationType new];
        [self.view addSubview:self.secondView];
        [self.secondView startRotateAnimated:NO];
        
        // 秒钟图片
        UIImageView *secondView          = [[UIImageView alloc] initWithFrame:self.secondView.bounds];
        secondView.image                 = [UIImage imageNamed:@"sec"];
        [self.secondView addSubview:secondView];
        
        secondView.glowRadius            = @(2.f);
        secondView.glowOpacity           = @(0.75f);
        secondView.glowColor             = [[UIColor cyanColor] colorWithAlphaComponent:1.f];
        
        secondView.glowDuration          = @(1.f);
        secondView.hideDuration          = @(1.f);
        secondView.glowAnimationDuration = @(1.f);
        
        [secondView createGlowLayer];
        [secondView insertGlowLayer];
        [secondView startGlowLoop];
    }

    {
        // 表盘
        UIView *circleRound            = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250.f, 250.f)];
        circleRound.layer.cornerRadius = 250 / 2.f;
        circleRound.layer.borderColor  = [UIColor blackColor].CGColor;
        circleRound.layer.borderWidth  = 2.f;
        circleRound.center             = self.view.center;
        [self.view addSubview:circleRound];
        
        // 中心红点
        UIView *circle            = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 6)];
        circle.layer.cornerRadius = 6 / 2.f;
        circle.backgroundColor    = [UIColor redColor];
        circle.center             = self.view.center;
        [self.view addSubview:circle];
    }
    
    self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [self.timer event:^{
        
        _secondCount                    += ONE_SEC;
        self.secondView.fromCircleRadian = self.secondView.toCircleRadian;
        self.secondView.toCircleRadian   = _secondCount;
        [self.secondView startRotateAnimated:YES];
        
        _minuteCount                    += ONE_MIN;
        self.minuteView.fromCircleRadian = self.minuteView.toCircleRadian;
        self.minuteView.toCircleRadian   = _minuteCount;
        [self.minuteView startRotateAnimated:YES];
        
        _hourCount                    += ONE_HOUR;
        self.hourView.fromCircleRadian = self.hourView.toCircleRadian;
        self.hourView.toCircleRadian   = _hourCount;
        [self.hourView startRotateAnimated:YES];
        
    } timeIntervalWithSecs:1.f];
    [self.timer start];
    
    [self bringTitleViewToFront];
}

@end

细节


目录
相关文章
|
3月前
|
前端开发 JavaScript
构建一个动态时钟
构建一个动态时钟
|
8月前
UE4 动画蓝图中两类动画曲线的使用
UE4 动画蓝图中两类动画曲线的使用
81 1
UE4 动画蓝图中两类动画曲线的使用
|
10月前
|
前端开发 JavaScript
【Three.js入门】渲染第一个场景及物体(轨道控制器、坐标轴辅助器、移动缩放旋转)
【Three.js入门】渲染第一个场景及物体(轨道控制器、坐标轴辅助器、移动缩放旋转)
202 0
|
移动开发 前端开发 Shell
使用canvas绘制时钟
使用canvas绘制时钟
59 0
JM
|
算法 数据可视化 C++
修改 UE5 中的渲染管线
前言本文重点介绍如何修改 UE5 中的渲染管线,要修改渲染管线有一些前置知识需要理解,因此笔者会先简单介绍下渲染管线的概念以及当前主流的渲染管线的实现思路,为后面在 UE5 中自定义渲染管线做铺垫;要注意本文默认渲染管线即是光栅化渲染管线(不考虑光线追踪),同时也不会介绍太多管线的实现细节和当下流行的优化版本,对渲染管线实现细节感兴趣的可以自行查阅相关资料。渲染管线 Rendering Pipel
JM
2325 0
修改 UE5 中的渲染管线
|
JavaScript 前端开发 算法
html+css+JavaScript实现简洁的圆形时钟数字时钟+指针时钟
使用前端三件套实现一个实时跟新的时钟,其中包括电子时钟和刻度时钟
234 0
html+css+JavaScript实现简洁的圆形时钟数字时钟+指针时钟
|
数据采集 编解码 数据处理
案例分享:Qt高频fpga采集数据压力位移速度加速度分析系统(通道配置、电压转换、采样频率、通道补偿、定时采集、距离采集,导出excel、自动XY轴、隐藏XY轴、隐藏显示通道,文件回放等等)
案例分享:Qt高频fpga采集数据压力位移速度加速度分析系统(通道配置、电压转换、采样频率、通道补偿、定时采集、距离采集,导出excel、自动XY轴、隐藏XY轴、隐藏显示通道,文件回放等等)
案例分享:Qt高频fpga采集数据压力位移速度加速度分析系统(通道配置、电压转换、采样频率、通道补偿、定时采集、距离采集,导出excel、自动XY轴、隐藏XY轴、隐藏显示通道,文件回放等等)
|
数据采集 编解码 开发者
案例分享:Qt多通道数据采集系统(通道配置、电压转换、采样频率、通道补偿值、定时采集、导出excel和图表、自动XY轴、隐藏XY轴、实时隐藏显示通道
案例分享:Qt多通道数据采集系统(通道配置、电压转换、采样频率、通道补偿值、定时采集、导出excel和图表、自动XY轴、隐藏XY轴、实时隐藏显示通道
案例分享:Qt多通道数据采集系统(通道配置、电压转换、采样频率、通道补偿值、定时采集、导出excel和图表、自动XY轴、隐藏XY轴、实时隐藏显示通道
通过定时器T1查询方式控制LED1周期性闪烁(自由计数模式)
通过定时器T1查询方式控制LED1周期性闪烁(自由计数模式) 宏定义与函数声明 初始化 主函数
253 0