一、午夜倒数《苹果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

posted @ 2015-07-26 14:46  锦夏ing  阅读(586)  评论(0编辑  收藏  举报