如何动态绘制时钟

如何动态绘制时钟

 

效果

 

源码

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

细节

时间: 2024-08-11 03:47:21

如何动态绘制时钟的相关文章

如何使用JFrame完成动态模拟时钟_java

这篇文章介绍了使用JFrame完成动态模拟时钟,在面板中绘制时钟并提取系统当前时刻,主方法中暂停线程1秒,刷新面板. 实现代码如下 import javax.swing.*; import java.awt.*; import java.util.*; import java.lang.Thread; import java.text.DecimalFormat; public class StillClock extends JPanel { /** * @param args */ priv

【UWP通用应用开发】编辑文本、绘制图形、3D透视效果及绘制时钟实战

编辑文本及键盘输入 相信大家都会使用TextBox,但如果要让文本在TextBox中换行该怎么做呢?将TextWrapping属性设置为Wrap,将AcceptsReturn属性设置为True就好咯. PasswordBox很明显就是一个密码框了,和其他的控件相比其有2个特殊之处,一个是其可以用MaxLength来控制最大的长度,一个是用PasswordChanged来捕捉密码的改名.显然比如QQ密码的MaxLength就是16位了,而PasswordChanged可以用来监测比如用户设置的密码

java-Java图形界面编程怎样动态绘制矩形?

问题描述 Java图形界面编程怎样动态绘制矩形? 就是类似Windows自带画图程序的那种,从一个点拖动到另一个点, 动态地绘制一个任意大小的矩形,大概的思路是怎样的 如果有其它语言实现的,可以讲一下大概的思路啊,不一定要代码 解决方案 '先确定要画的是矩形,在获取鼠标按下点的坐标,在获取移动到的点的坐标,实现移动一次图形更新一次,最后确定下鼠标抬起点的坐标,用以确定最后矩形的的大小 解决方案二: 这里有个完整的例子:http://developer.51cto.com/art/201201/3

Qt之绘制时钟

简述 QPainter 提供了 2D 绘图的常用操作,QTimer 提供了定时器功能,将两者相结合,可以做出很多的自定义特效绘制. 下面,来实现一个每天都要接触的东西 - 时钟.包含了常见的所有功能:时针.分针.秒针... 简述 实现方式 示例 效果 源码 实现方式 由于时钟是妙级更新的,所以我们需要定时刷新,时钟本身则使用之前讲过的 QPainter 来进行绘制. 使用 QTimer 定时刷新,设置超时时间为 1000 毫秒(1 秒). 绘制时钟,通过 paintEvent() 实现,包括:时

html5使用canvas绘制时钟示例

准备工作 在HTML中指定一个区域放置时钟: <div id="clock" style="position: relative;"></div> 时钟的一些外观设定: var width = 260; // 桌布宽度 var height= 260; // 桌布高度 var dot = {     x : width / 2,     y : height / 2,     radius : 6 }; // 圆点位置.半径 var radi

JavaScript html5 canvas绘制时钟效果_javascript技巧

本文实例讲述了JavaScript+html5 canvas绘制时钟效果.分享给大家供大家参考,具体如下:  HTML部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.

JavaScript html5 canvas绘制时钟效果(二)_javascript技巧

 对于H5来说,canvas可以说是它最有特色的一个地方了,有了它之后我们可以随意的在网页上画各种各样的图形,做一些小游戏啊什么的.canvas这个标签的用法,在网上也有特别多的教程了,这里就不作介绍了.今天我们就用canvas来做一个小小的时钟.完整的代码在这里https://github.com/wwervin72/HTML5-Clock. 那么首先在这个页面里面我使用了两个canvas,一个用来绘制静态的时钟表盘和刻度,另一个用来绘制时钟的三个指针,然后用定位让他们重合到一起.然后这里没什

JS+Canvas绘制时钟效果_javascript技巧

本文实例为大家分享了使用canvas绘制时钟的具体代码,供大家参考,具体内容如下 1. clock.html    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Description" content=""> <title>canvas时钟</t

javascript入门&amp;#183;动态的时钟,显示完整的一些方法,新年倒计时_时间日期

时间对象作为非常重要的一个对象,对我们学.net的人来说,并不是很重要,但这并不意味着我们可以忽略,事实上,用得着的时候还是很多的,如果完全依赖JS处理时间,那是会出问题的,因为JS总是假设本地机器上的时间是正确的.还有个原因,他总按照GTM市区来计量.我们只是返回当前date对象的副本,我们即便是修改,那也不会对对象本身有任何影响. 演示一:动态的时钟(来个复杂的) 11:55:13 演示二:显示完整的一些方法(事实上我很讨厌有些格式了) Mon Oct 1 22:35:25 UTC+0800