数字钟的创建,我们首先的准备一些参数:代码如下:
#import "AppDelegate.h"
@implementation AppDelegate
{
UILabel * _yearLabel;
UILabel * _monthLabel;
UILabel * _dayLabel;
UILabel * _hourLabel;
UILabel * _minuteLabel;
UILabel * _secondLabel;
NSMutableArray * _dateArray;
NSTimer * _timer;
}
1.创建一个数组,用来存放,年,月,日,时,分,秒
-(void)prepareDataArray
{
_dateArray = [[NSMutableArray alloc]initWithObjects:@"年",@"月",@"日",@"时",@"分",@"秒", nil];
}
-(void)prepareUI
{
for (int i = 0; i<2; i++) {
for (int j = 0; j<3; j++) {
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(70+100*j, 50+i*70, 50, 50)];
label.backgroundColor = [UIColor cyanColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = [_dateArray objectAtIndex:i*3+j];
label.font = [UIFont fontWithName:@"LcdD" size:35];
[self.window addSubview:label];
[label release];
}
}
//设置年
_yearLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 70, 50)];
_yearLabel.text = @"0000";
_yearLabel.textColor = [UIColor redColor];
_yearLabel.textAlignment = NSTextAlignmentCenter;
_yearLabel.font = [UIFont fontWithName:@"LcdD" size:30];
_yearLabel.backgroundColor = [UIColor yellowColor];
[self.window addSubview:_yearLabel];
[_yearLabel release];
//设置月
_monthLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 50, 50, 50)];
_monthLabel.text = @"00";
_monthLabel.textColor = [UIColor redColor];
_monthLabel.textAlignment = NSTextAlignmentCenter;
_monthLabel.font = [UIFont fontWithName:@"LcdD" size:30];
_monthLabel.backgroundColor = [UIColor yellowColor];
[self.window addSubview:_monthLabel];
[_monthLabel release];
//设置日
_dayLabel = [[UILabel alloc]initWithFrame:CGRectMake(220, 50, 50, 50)];
_dayLabel.text = @"00";
_dayLabel.textColor = [UIColor redColor];
_dayLabel.textAlignment = NSTextAlignmentCenter;
_dayLabel.font = [UIFont fontWithName:@"LcdD" size:30];
_dayLabel.backgroundColor = [UIColor yellowColor];
[self.window addSubview:_dayLabel];
[_dayLabel release];
//设置时
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 120, 70, 50)];
view.backgroundColor = [UIColor yellowColor];
[self.window addSubview:view];
[view release];
_hourLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 40, 50)];
_hourLabel.text = @"00";
_hourLabel.textColor = [UIColor redColor];
_hourLabel.textAlignment = NSTextAlignmentCenter;
_hourLabel.font = [UIFont fontWithName:@"LcdD" size:30];
_hourLabel.backgroundColor = [UIColor yellowColor];
[view addSubview:_hourLabel];
[_hourLabel release];
//设置分
_minuteLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 120, 50, 50)];
_minuteLabel.text = @"00";
_minuteLabel.textColor = [UIColor redColor];
_minuteLabel.textAlignment = NSTextAlignmentCenter;
_minuteLabel.font = [UIFont fontWithName:@"LcdD" size:30];
_minuteLabel.backgroundColor = [UIColor yellowColor];
[self.window addSubview:_minuteLabel];
[_minuteLabel release];
//设置秒
_secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(220, 120, 50, 50)];
_secondLabel.text = @"00";
_secondLabel.textColor = [UIColor redColor];
_secondLabel.textAlignment = NSTextAlignmentCenter;
_secondLabel.font = [UIFont fontWithName:@"LcdD" size:30];
_secondLabel.backgroundColor = [UIColor yellowColor];
[self.window addSubview:_secondLabel];
[_secondLabel release];
}
再者,我们获取现在时间。。。
//获取时间 ,返回一时间 字符串
-(NSString *)getDate
{
NSDate * date = [NSDate date];
NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy MM dd HH mm ss"];
//返回格式化后的时间字符串
return [dateFormat stringFromDate:date];
}
yyyy MM dd HH mm ss为数字中显示的样式。。。。
在然后。我们给参数赋值,为现在时间,并显示。。。代码如下
<pre name="code" class="objc">-(void)startTimer
{
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
}
-(void)changeTime
{
NSString * string = [self getDate];
NSArray * dateArray = [string componentsSeparatedByString:@" "];
_yearLabel.text = [dateArray objectAtIndex:0];
_monthLabel.text = [dateArray objectAtIndex:1];
_dayLabel.text = [dateArray objectAtIndex:2];
_hourLabel.text = [dateArray objectAtIndex:3];
_minuteLabel.text = [dateArray objectAtIndex:4];
_secondLabel.text = [dateArray objectAtIndex:5];
}
原文地址:http://blog.csdn.net/zhoushuangjian511/article/details/42104347