一、午夜倒数《苹果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较为深入的内容 ...
随机推荐
- 攻城狮在路上(叁)Linux(二十四)--- linux设置开机挂载及镜像文件挂载
虽然可以手动进行文件系统的挂载,但是每次都手动挂载就会很麻烦,开机挂载的目的就是实现文件系统的自动挂载. 一.开机挂载:/etc/fstab及/etc/mtab 主要是通过修改/etc/fstab文件 ...
- [Linux] 获得系统位数
三种方法获得系统多少位: 以下三个例子都得到的是64位的系统 1. getconf LONG_BIT 2. echo $HOSTTYPE 3. uname –a
- 笔记本电脑关闭小键盘(即打字按P出现星号键)
开关方法:Fn + NumLk (联想电脑的NumLk 一般为F8,其他电脑自己在键盘找找罗)
- Eclipse调试方法及快捷键
基本操作 断点,breakpoint: F5键与F6键均为单步调试: F5是step into,也就是进入本行代码中执行,跳入 F6是step over,跳过,也就是执行本行代码,跳到下一行 F7是跳 ...
- Java学习随笔3:遍历文件夹及文件的读取和写入
import java.io.File; /** * 遍历文件夹 */ public class ScannerFile { public static void main(String[] args ...
- ie上如何兼容placeholder
首先,判断浏览器是否支持placeholder属性: var input = document.createElement('input'); if("placeholder" i ...
- 【架构】MVP模型
MVP模型一般要创建三个文件夹:View.Interactor(Model).Presenter 每个部分都有其接口和实现类,就是为了方便回调 这里做一个登陆界面为例子: 接口: Interactor ...
- MPAndroidChart饼图属性及相关设置
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...
- psql-01基本介绍
安装与启动 安装: apt-get install postgresql / yum install postgresql.XXX; 启动: mac下直接打开 linux service postgr ...
- BZOJ 3932 [CQOI2015]任务查询系统 ——可持久化线段树
[题目分析] 主席树,维护区间大小以及权值之和. 但是细节确实要琢磨很久,WA了几次. [代码] #include <cstdio> #include <cstring> #i ...