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

(素材源码)swanzhu学IOS(二)UI之_NSTimer

时间:2015-08-06 22:20:12      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

//
//  ZYViewController.m
//  SelectorTime
//
//  Created by mac on 15-8-3.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//

#import "ZYViewController.h"

@interface ZYViewController ()
{
//    全局变量
    NSTimer *timer;
}
@end

@implementation ZYViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
//思路
//1  创建5个label用来显示数字和冒号
//2  开启计时器(设置三个计数器:秒计数器,分钟计数器,小时计数器)使用拼接字符串的方法让数字显示在label上;
//3  创建nstimer
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UILabel *label1=[[UILabel alloc] initWithFrame:CGRectMake(40, 100, 60, 60)];
    label1.text=@"00";
    label1.tag=1;
    label1.backgroundColor=[UIColor blueColor];
    label1.textAlignment=NSTextAlignmentCenter;
    label1.textColor=[UIColor whiteColor];
    [self.view addSubview:label1];
    UILabel *label2=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 10, 60)];
    label2.text=@":";
    [self.view addSubview:label2];
    
    UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(110, 100, 60, 60)];
    label3.text=@"00";
    label3.tag=3;
    label3.backgroundColor=[UIColor blueColor];
    label3.textColor=[UIColor whiteColor];
    label3.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:label3];
    
    
    UILabel *label4=[[UILabel alloc] initWithFrame:CGRectMake(170, 100, 10, 60)];
    label4.text=@":";
    [self.view addSubview:label4];
    
    
    UILabel *label5=[[UILabel alloc] initWithFrame:CGRectMake(180, 100, 60, 60)];
    label5.text=@"00";
    label5.tag=5;
    label5.backgroundColor=[UIColor blueColor];
    label5.textAlignment=NSTextAlignmentCenter;
    label5.textColor=[UIColor whiteColor];
    [self.view addSubview:label5];
    
    
    //    开启定时器
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    
    btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame=CGRectMake(50, 300, 50, 50);
    btn1.backgroundColor=[UIColor redColor];
    [btn1 setTitle:@"暂停" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame=CGRectMake(130, 300, 50, 50);
    btn2.backgroundColor=[UIColor redColor];
    [btn2 setTitle:@"清零" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
}

-(void)click{

    open=!open;
    if (open==YES) {
        [btn1 setTitle:@"开始" forState:UIControlStateNormal];
        [timer invalidate];
        timer=nil;
        
    }else{
        [btn1 setTitle:@"暂停" forState:UIControlStateNormal];
      timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        
    }
}
-(void)onClick{

    UILabel *label1=(UILabel *)[self.view viewWithTag:1];
   UILabel *label3=(UILabel *)[self.view viewWithTag:3];
   UILabel *label5=(UILabel *)[self.view viewWithTag:5];
    label1.text=label3.text=label5.text=@"00";
    sCount=mCount=hCount=0;
}

-(void)onTimer{
    //viewWithTag  通过tag来寻找控件
    //    一定要在父视图查找
    UILabel *label1=(UILabel *)[self.view viewWithTag:1];
    UILabel *label3=(UILabel *)[self.view viewWithTag:3];
    UILabel *label5=(UILabel *)[self.view viewWithTag:5];
    //    改变标题
    sCount++;
    
    
    if (sCount==59) {
        sCount=0;
        mCount++;
        [self setLabelAnimations:label3];
        label3.text=[NSString stringWithFormat:@"%d",mCount];
        
        if (mCount==59) {
            mCount=0;
            hCount++;
            [self setLabelAnimations:label1];
            label1.text=[NSString stringWithFormat:@"%d",hCount];
        }
    }
    label5.text=[NSString stringWithFormat:@"%d",sCount];
    //    2做动画     封装动画(label做动画,把label传到动画中)
    [self setLabelAnimations:label5];
   
    
}

-(void)setLabelAnimations:(UILabel *)labels{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.9];
    [UIView setAnimationTransition:6 forView:labels cache:YES];
    [UIView commitAnimations];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

素材源码  :http://download.csdn.net/detail/swanzhu/8957883


版权声明:本文为博主原创文章,未经博主允许不得转载。

(素材源码)swanzhu学IOS(二)UI之_NSTimer

标签:

原文地址:http://blog.csdn.net/swanzhu/article/details/47323779

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