(素材源码)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)];
    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


基于STM32F407,使用DFS算法实现最短迷宫路径检索,分为三种模式:1.DEBUG模式,2. 训练模式,3. 主程序模式 ,DEBUG模式主要分析bug,测量必要数据,训练模式用于DFS算法训练最短路径,并将最短路径以链表形式存储Flash, 主程序模式从Flash中….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、习资料等,我会提供帮助,提供资料,鼓励习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等科竞赛比赛、初期项目立项、习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源习/技术交流/习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值