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

//
//  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)];
    [email protected]"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)];
    [email protected]":";
    [self.view addSubview:label2];

    UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(110, 100, 60, 60)];
    [email protected]"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)];
    [email protected]":";
    [self.view addSubview:label4];

    UILabel *label5=[[UILabel alloc] initWithFrame:CGRectMake(180, 100, 60, 60)];
    [email protected]"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];
    [email protected]"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

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

时间: 2024-08-26 10:08:30

(素材源码)swanzhu学IOS(二)UI之_NSTimer的相关文章

(素材源码)猫猫学iOS(四十六)之网易彩票幸运大转盘

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 素材源码地址:http://download.csdn.net/detail/u013357243/8713827 效果 代码: NYWheel NYWheel.h // // NYWheel.h // 网易彩票幸运大转盘 // // Created by apple on 15-5-18. // Copyright (c)

(素材源码)猫猫学IOS(二十六)UI之iOS抽屉效果小Demo

猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8635679 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果 源码 NYDrawViewController.h // // NYDrawViewController.h // 06-抽屉效果 // // Created by apple on 14-9-1. /

(素材源码)猫猫学IOS(四十一)UI之核心动画 两行代码搞定3D转场(做android的哭死)

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 源码素材地址:http://download.csdn.net/detail/u013357243/8677065 效果: 代码: #import "NYViewController.h" @interface NYViewController () @property (weak, nonatomic) IBOut

(素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 源码:http://download.csdn.net/detail/u013357243/8669765 效果: 代码: NYLockView.h // // NYLockView.h // 手势解锁 // // Created by apple on 15-5-6. // Copyright (c) 2015年 znyca

(素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 源码:http://download.csdn.net/detail/u013357243/8666923 效果: 代码: NYView NYView.h // // NYView.h // 画画板 // // Created by apple on 15-5-6. // Copyright (c) 2015年 znycat.

(素材源码)猫猫学IOS(三十六)UI之手势事件旋转_缩放_拖拽

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 源码:http://download.csdn.net/detail/u013357243/8671943 效果 完成一个图片的捏合缩放,拖拽,旋转动作. 代码:NYViewController.m // // NYViewController.m // 旋转_缩放_拖拽 // // Created by apple on 1

(素材源码) 猫猫学IOS(十二)UI之UITableView学习(上)LOL英雄联盟练习

猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8542789 原文地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧. 原文:http://blog.csdn.net/u013357243?viewmode=contents

(素材源码)猫猫学IOS(二十四)UI之注册案例

猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8614731 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果 代码 NYViewController.m // // NYViewController.m // 注册 // // Created by apple on 15-4-19. // Copyright

(素材源码)猫猫学IOS(二十)UI之UIPickerView_点菜系统

猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8596279 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 ## 代码 // // NJViewController.m // 01-点菜 // // Created by apple on 14-6-3. // Copyright (c) 2014年 hei

(素材源码)猫猫学IOS(二十九)UI之Quartz2D自定义下载控件

猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8640353 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 效果 代码 NYProgressView.m // // NYProgressView.m // 下载进度条 // // Created by apple on 15-4-27. // Copyright (c)