[翻译] ColourClock 将时间值转换成背景色
ColourClock 将时间值转换成背景色
https://github.com/bennyguitar/ColourClock
This project converts Time to Hex/RGB, and is quite beautiful to look at. This was HEAVILY inspired byhttp://thecolourclock.co.uk and really, all credit goes to them.
这个工程是用来把时间值转换为Hex/RGB值的,看起来非常漂亮。灵感来自于这个网站 http://thecolourclock.co.uk
使用思路:
将一个要根据时间改变颜色View中layer的backgroundcolor赋值即可动态改变颜色。
附录:
ViewController.h
//
// ViewController.h
// ColourClock
//
// Created by Ben Gordon on 12/20/12.
// Copyright (c) 2012 Ben Gordon. All rights reserved.
// #import <UIKit/UIKit.h> enum ClockType {
ClockTypeMilitary = ,
ClockTypeHex = ,
ClockTypeRGB =
}; @interface ViewController : UIViewController { __weak IBOutlet UILabel *timeLabel;
__weak IBOutlet UILabel *appearanceType; enum ClockType currentType;
} - (IBAction)changeClockType:(id)sender; @end
ViewController.m
//
// ViewController.m
// ColourClock
//
// Created by Ben Gordon on 12/20/12.
// Copyright (c) 2012 Ben Gordon. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController #pragma mark - View Lifecycle - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. currentType = ClockTypeMilitary;
[self changeColor]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Change Colors -(void)changeColor {
// Set up date formatters for hour, min, seconds.
// Then create strings from the current date.
NSDateFormatter *formatHour = [[NSDateFormatter alloc] init];
NSDateFormatter *formatMin = [[NSDateFormatter alloc] init];
NSDateFormatter *formatSec = [[NSDateFormatter alloc] init];
[formatHour setDateFormat:@"HH"];
[formatMin setDateFormat:@"mm"];
[formatSec setDateFormat:@"ss"];
NSString *hour = [formatHour stringFromDate:[NSDate date]];
NSString *minute = [formatMin stringFromDate:[NSDate date]];
NSString *second = [formatSec stringFromDate:[NSDate date]]; // Create floats of the time value.
float hourFloat = [hour floatValue] * 255.0f / 23.0f;
float minFloat = [minute floatValue] * 255.0f / 59.0f;
float secFloat = [second floatValue] * 255.0f / 59.0f; // Create unsigned ints for Hex translation
int32_t hourint = hourFloat + 0.5;
int32_t minint = minFloat + 0.5;
int32_t secint = secFloat + 0.5; // Change text color so it's readable.
if (hourFloat > && minFloat > && secFloat > ) {
timeLabel.textColor = [UIColor darkGrayColor];
appearanceType.textColor = [UIColor darkGrayColor];
}
else {
timeLabel.textColor = [UIColor whiteColor];
appearanceType.textColor = [UIColor whiteColor];
} // Set Labels
if (currentType == ClockTypeMilitary) {
appearanceType.text = @"MILITARY TIME";
timeLabel.text = [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second];
}
else if (currentType == ClockTypeHex) {
appearanceType.text = @"HEX COLOR CODE";
timeLabel.text = [NSString stringWithFormat:@"#%02X%02X%02X",hourint,minint,secint];
}
else {
appearanceType.text = @"RGB VALUES";
timeLabel.text = [NSString stringWithFormat:@"%.0f:%.0f:%.0f", hourFloat, minFloat, secFloat];
} // Finally, change image to the right color
self.view.backgroundColor = [UIColor colorWithRed:(hourFloat/255.0f) green:(minFloat/255.0f) blue:(secFloat/255.0f) alpha:1.0]; // And do it all over again, every .05 seconds so it's more accurate
[self performSelector:@selector(changeColor) withObject:nil afterDelay:0.05];
} #pragma mark - Change Clock Type - (IBAction)changeClockType:(id)sender {
currentType++; if (currentType > ClockTypeRGB) {
currentType = ClockTypeMilitary;
}
} @end
[翻译] ColourClock 将时间值转换成背景色的更多相关文章
- ios 把毫秒值转换成日期 NSDate
ios 把毫秒值转换成日期 (比较好用) 1343359790000 这是毫秒值------最佳解决方案-------------------- long long time=134335979000 ...
- Web API-如何将Controller的返回值转换成HTTP response消息
https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...
- sql 在将 nvarchar 值 转换成数据类型 int 时失败。
假设有存储过程:proc_test2 create proc proc_test2 @Id int begin as declare @sql varchar(max) @sql = 'select ...
- json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值
一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...
- Jquery把获取到的input值转换成json
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQL3120W 不能将xx的字段值转换成 INTEGER值
一次用DB2 Load/Import导入数据时,报错,提示SQL3120W 不能将xx的字段值转换成 INTEGER值,但目标列不可为空.未装入该行. 目标表: CREATE TABLE TEST( ...
- c# 科学计数法值转换成正常值,返回字符串
/// <summary> /// 科学计数法值转换成正常值 /// </summary> /// <param name="value">&l ...
- 将数据库中的内容展示出来并将某些value值转换成汉字
1.将数据库中的内容展示出来 前台代码未做改变,刚开始未显示的原因是因为 data-field 跟数据库不一样data-field 需要跟数据库中的一样才可以 2.将某些value值转换成汉字 在li ...
- sql server like 在将值转换成数据类型int失败
select * from table where title like '%'?'%'; 采用? 传参会报错:sql server like 在将值转换成数据类型int失败 select * fro ...
随机推荐
- 【PAT】1001. A+B Format (20)
1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits m ...
- 【LOJ】#2350. 「JOI 2017/2018 决赛」月票购买
题解 首先求一个最短路图出来,最短路图就是这条边在最短路上就保留,否则就不保留,注意最短路图是一个有向图,一条边被保留的条件是 dis(S,u) + val(u,v) = dis(v,T)我们需要求两 ...
- bzoj 2326 矩阵快速幂
思路:矩阵快速幂搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second # ...
- 双缓冲解决控制台应用程序输出“闪屏”(C/C++,Windows)
使用 C 语言编写游戏的小伙伴们想必起初都要遇到这样的问题,在不断清屏输出数据的过程中,控制台中的输出内容会不断地闪屏.出现这个问题的原因是程序对数据处理花掉的时间影响到了数据显示,或许你可以使用局部 ...
- ref:Struts2 命令执行系列回顾
ref:http://www.zerokeeper.com/vul-analysis/struts2-command-execution-series-review.html Struts2 命令执行 ...
- Docker入门到实战
1.系统要求 Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10. CentOS 7满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2 存 ...
- vim自动补全插件YouCompleteMe的安装及配置
原文地址: http://blog.csdn.net/shixuehancheng/article/details/46289811
- Python字典树实现
class Trie: # word_end = -1 def __init__(self): """ Initialize your data structure he ...
- [转]Intel haxm安装失败问题解决
在安装Intel haxm为安卓模拟器加速时,会遇到提示VT-X未开启问题,问题提示如下图 工具/原料 Intel haxm 安卓模拟器 方法/步骤 1 确认你的处理器是否是Intel的,如果是AMD ...
- python输出字符串,UnicodeEncodeError: 'ascii' codec can't encode characters in position问题
2017-06-28更新:换到python3.x中,编码问题减少了很多.这篇博文不适用于python3.x http://blog.sina.com.cn/s/blog_64a3795a01018vy ...