如何动态绘制时钟

简介:

如何动态绘制时钟

 

效果

 

源码

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

细节


目录
相关文章
图形时钟
#include #include #include #define pi 3.1415926void draw(int a, int b, int c){    float x, y;    x = a * cos(b *c * pi / 180-pi / 2) + 300;      /*确定横...
607 0
|
数据采集 编解码 开发者
案例分享:Qt多通道数据采集系统(通道配置、电压转换、采样频率、通道补偿值、定时采集、导出excel和图表、自动XY轴、隐藏XY轴、实时隐藏显示通道
案例分享:Qt多通道数据采集系统(通道配置、电压转换、采样频率、通道补偿值、定时采集、导出excel和图表、自动XY轴、隐藏XY轴、实时隐藏显示通道
|
数据采集 编解码 数据处理
案例分享:Qt高频fpga采集数据压力位移速度加速度分析系统(通道配置、电压转换、采样频率、通道补偿、定时采集、距离采集,导出excel、自动XY轴、隐藏XY轴、隐藏显示通道,文件回放等等)
案例分享:Qt高频fpga采集数据压力位移速度加速度分析系统(通道配置、电压转换、采样频率、通道补偿、定时采集、距离采集,导出excel、自动XY轴、隐藏XY轴、隐藏显示通道,文件回放等等)
通过定时器T1查询方式控制LED1周期性闪烁(模模式)
通过定时器T1查询方式控制LED1周期性闪烁(模模式) 宏定义与函数声明 初始化 主函数
332 0
|
7月前
|
数据可视化 开发者 C++
Qt(C++)使用QChart静态显示3个设备的温度变化曲线
QChart模块是Qt Charts库的基础,提供了用于创建和显示各种类型图表的类和接口。Qt Charts库是一个功能丰富、易于使用的数据可视化工具库,可以帮助开发者在应用程序中添加漂亮而又交互性强的图表。
105 1
Qt(C++)使用QChart静态显示3个设备的温度变化曲线
Qt之绘制时钟
简述 QPainter 提供了 2D 绘图的常用操作,QTimer 提供了定时器功能,将两者相结合,可以做出很多的自定义特效绘制。 下面,来实现一个每天都要接触的东西 - 时钟。包含了常见的所有功能:时针、分针、秒针。。。 简述 实现方式 示例 效果 源码 实现方式 由于时钟是妙级更新的,所以我们需要定时刷新,时钟本身则使用之前讲过的 QP
2615 0
|
9月前
|
Java
JavaSwing实现动态时钟【风格2】
JavaSwing实现动态时钟【风格2】
JavaSwing实现动态时钟【风格1】
JavaSwing实现动态时钟【风格1】
通过定时器T1查询方式控制LED1周期性闪烁(自由计数模式)
通过定时器T1查询方式控制LED1周期性闪烁(自由计数模式) 宏定义与函数声明 初始化 主函数
294 0

热门文章

最新文章