一、午夜倒数《苹果iOS实例编程入门教程》
该app为应用的功能为计算离午夜12:00点的剩余时间
现版本 SDK 8.4 Xcode
运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 minutesToMidnight
(1) 在xCode打开 ViewController.h 文件
(红色为所添加的代码)
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSTimer *timer;
IBOutlet UILabel *countdownLabel;
}
@property (nonatomic,retain)NSTimer *timer;
@property (nonatomic,retain)IBOutlet UILabel *countdownLabel;
-(void)onTimer;
@end
(2) 在xCode打开 ViewController.m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize timer;
@synthesize countdownLabel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[countdownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:33]];// 字体 大小
countdownLabel.text = @"00:00:00";// 初始化Label的值
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
/*
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
预订一个Timer,设置一个时间间隔。表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1
target:(id)aTarget
表示发送的对象,如self
selector:(SEL)aSelector
方法选择器,在时间间隔内,选择调用一个实例方法
userInfo:(id)userInfo
此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。
repeats:(BOOL)yesOrNo
当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。
*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)onTimer
{
NSDate *now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
/* NSDate -- 表示一个绝对的时间点
NSTimeZone -- 时区信息
NSLocale -- 本地化信息
NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类
NSCalendar -- 日历类,它提供了大部分的日期计算接口,并且允许您在NSDate和NSDateComponents之间转换
NSDateFormatter -- 用来在日期和字符串之间转换
//****NSCalendar 日历类****//
创建或初始化可用以下方法
+ (id)currentCalendar;
取得当前用户的逻辑日历(logical calendar)
+ (id)autoupdatingCurrentCalendar;
取得当前用户的逻辑日历(logical calendar), ......
- (id)initWithCalendarIdentifier:(NSString *)identifier;
初始化为各种日历。identifier的范围可以是:
NSCalendarIdentifierGregorian 阳历
NSCalendarIdentifierBuddhist 佛历
NSCalendarIdentifierChinese 中国日历
NSCalendarIdentifierHebrew 希伯来日历
NSCalendarIdentifierIslamic 伊斯兰日历
NSCalendarIdentifierIslamicCivil 伊斯兰民事日历
NSCalendarIdentifierJapanese 日本日历
*/
NSDateComponents *dateComponents = [gregorian components:(kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond) fromDate:now];
NSInteger hour = [dateComponents hour];
NSInteger min = [dateComponents minute];
NSInteger sec = [dateComponents second];
sec = 60 -sec;
min = 59-min;
hour = 23 - hour;
countdownLabel.text = [NSString stringWithFormat:@"%ld:%ld:%ld",hour,min,sec];
}
@end
(3) UIView 界面设置
- 黑色背景
点击Main.storyboard
选择view controller ->view
将背景设为黑色
选择Attributes Inspector->Background->Black Color
(4) 加入 UILabel 显示倒数时间
- 红色字体
选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Label 到 Main View
从Label Attributes 上选着字体的颜色和字体大小
(5)写入 UILabel 的 class file
鼠标右击Label控件,鼠标移动到"New Referencing Outlet" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "countdownLabel"; 选上它。
选择: File -> Save
最后在 xCode 选择 Build and then Running
(6)模拟器效果图

本文源于网上一博客教程,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100djg2.html
一、午夜倒数《苹果iOS实例编程入门教程》的更多相关文章
- 七、考反映小游戏《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...
- 六、雪花《苹果iOS实例编程入门教程》
该app为应用的功能为制作一场雪景 现版本 SDK 8.4 Xcode 纲要:- UIImageView 的运用- onTimer 代码运用- onAnimation 代码运用 运行Xcode 选择 ...
- 五、点数器《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...
- 四、卫星定位《苹果iOS实例编程入门教程》
该app为应用的功能为用iPhone 显示你现在的位置 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...
- 三、图像移动《苹果iOS实例编程入门教程》
该app为应用的功能为动态移动动画 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applicati ...
- 二 、打开地图《苹果iOS实例编程入门教程》
该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...
- 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!
C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征. 如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...
- PHP面向对象(OOP)编程入门教程
面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行PHP的高级编程,对于提高 PHP编程能力和规划好Web开发构架都是非常有意义的.下面我们就 ...
- VS2010/MFC编程入门教程之目录和总结
鸡啄米的这套VS2010/MFC编程入门教程到此就全部完成了,虽然有些内容还未涉及到,但帮助大家进行VS2010/MFC的入门学习业已足够.以此教程的知识为基础,学习VS2010/MFC较为深入的内容 ...
随机推荐
- html5 基本布局+新标签+新选择器 + 线性渐变
html5 基本布局+新标签 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- WPF MVVM初体验
首先MVVM设计模式的结构, Views: 由Window/Page/UserControl等构成,通过DataBinding与ViewModels建立关联: ViewModels:由一组命令,可以绑 ...
- 趣味算法:字符串反转的N种方法(转)
老赵在反对北大青鸟的随笔中提到了数组反转.这的确是一道非常基础的算法题,然而也是一道很不平常的算法题(也许所有的算法深究下去都会很不平常).因为我写着写着,就写出来8种方法……现在我们以字符串的反转为 ...
- codeforce ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver! 线段树
B2. Shave Beaver! The Smart Beaver has recently designed and built an innovative nanotechnologic a ...
- I/O复用模型之select学习
linux下的I/O模型可以分为5种: 1.阻塞式I/O模型 2.非阻塞式I/O模型 3.I/O复用模型 4.信号驱动I/O模型 5.异步I/O模型 简单解释: 阻塞和非阻塞:就是说需要做一件事的时候 ...
- mac上创建MySQL的基本步骤
首先得安装环境与MySQL的软件 安装环境的软件在这里我用的是:jdk-8u111-macosx-x64.dmg MySQL:mysql-5.7.16-osx10.11-x86_64.dmg 安装好了 ...
- python学习第二天
dict字典 把数据放入dict:直接赋值.初始化时指定 pop删除key set集合 add添加元素 remove删除元素 字符串str是不可变对象,对字符串的操作都会返回新的字符串 pass 什么 ...
- 简单用DOM4J结合XPATH解析XML
由于DOM4J在解析XML时只能一层一层解析,所以当XML文件层数过多时使用会很不方便,结合XPATH就可以直接获取到某个元素 使用dom4j支持xpath的操作的几种主要形式 第一种形式 ...
- Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法
Error -26488: Could not obtain information about submitted file "E:\.jpg": _stat32 rc=-1, ...
- MVC3实现多个按钮提交
有时我们需要在同一个Form表单中进行多个按钮的提交,来完成不同的功能.因为MVC中一个表单默认只能提交到一个Action处理,相对比较麻烦. 1.使用客户端脚本 我们可以view中加上如下代码: & ...