码迷,mamicode.com
首页 > 移动开发 > 详细

ios数字钟的实现

时间:2014-12-23 17:27:12      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:ios   ios数字钟      钟表   数字钟   

ios数字钟的实现


技术分享



   

数字钟的创建,我们首先的准备一些参数:代码如下:

#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];
}

2、我们创建界面,代码如下:

-(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];
    
}



效果展示如下:
技术分享
技术分享




 












ios数字钟的实现

标签:ios   ios数字钟      钟表   数字钟   

原文地址:http://blog.csdn.net/zhoushuangjian511/article/details/42104347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!