(Material source code) swanzhu learning IOS (2) 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 () {// global variable NSTimer * timer;} @ end @ implementation ZYViewController-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self ){ // Custom initialization} return self;} // Train of Thought // 1 create 5 labels to display numbers and enable the timer with the colon // 2 (set three counters: Second counters, minute counter, hour counter) use the concatenation string method to display the number on the label; // 3 create 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 = [UICol Or 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 = NSTextAlign MentCenter; [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 white Color]; [self. view addSubview: label5]; // enable the timer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (onTimer) userInfo: nil repeats: YES]; btn1 = [UIButton buttonWithType: callback]; btn1.frame = CGRectMake (50,300, 50, 50); btn1.backgroundColor = [UIColor redColor]; [btn1 setTitle: @ "pause" forState: UIControlStateNormal] [btn1 addTarget: self action: @ selector (click) forCont RolEvents: UIControlEventTouchUpInside]; [self. view addSubview: btn1]; btn2 = [UIButton buttonWithType: Hangzhou]; btn2.frame = CGRectMake (130,300, 50, 50); btn2.backgroundColor = [UIColor redColor]; [btn2 setTitle: @ "reset" 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: @ "pause" 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 uses tags to find controls. // you must find UILabel * label1 = (UILabel *) [self. view viewWithTag: 1]; UILabel * label3 = (UILabel *) [self. view viewWithTag: 3]; UILabel * label5 = (UILabel *) [self. view viewWithTag: 5]; // change the title 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. perform an animation encapsulation animation (label is used as an animation to upload the label to the animation) [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 didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
Material Source: http://download.csdn.net/detail/swanzhu/8957883
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.