(素材源碼)swanzhu學IOS(二)UI之_NSTimer,swanzhu_nstimer

來源:互聯網
上載者:User

(素材源碼)swanzhu學IOS(二)UI之_NSTimer,swanzhu_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


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.