貓貓學IOS(十)UI之_NSTimer_ios計時器,ios_nstimer_ios
貓貓分享,必須精品
素材代碼地址:http://blog.csdn.net/u013357243/article/details/44627787
原文地址:http://blog.csdn.net/u013357243?viewmode=contents
先看效果
代碼
原文地址:http://blog.csdn.net/u013357243?viewmode=contents//// NYViewController.m// 05 - 倒計時//// Created by apple on 15-3-25.// Copyright (c) 2015年 znycat. All rights reserved.//#import "NYViewController.h"@interface NYViewController () <UIAlertViewDelegate>@property (weak, nonatomic) IBOutlet UILabel *counterLabel;@property (nonatomic, strong) NSTimer *timer;@end@implementation NYViewController/**開始*/-(IBAction)start{// 倒計時10秒,計時器 /* NSTimer scheduledTimerWithTimeInterval 參數說明: 1,時間間隔,double 2,監聽時鐘觸發的對象 3,調用方法 4,userInfo,可以是任意對象,通常傳遞nil,如果有傳遞值,大多數是為了在多個計數器中分辨用哪個 5,repeats:是否重複執行調用方法。 */// scheduledTimerWithTimeInterval 方法本質上就是建立一個時鐘,// 添加到運行迴圈的模式是DefaultRunloopMode// _________________________________________________________________________________ //1>// self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];// 2> 與1一樣// self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];// //將timer添加到運行迴圈,模式:預設運行迴圈模式// [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];// __________________________________________________________________________________ //3> self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; //將timer添加到運行迴圈,模式:NSRunLoopCommonModes監聽滾動模式 [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];}/**每秒更新counterLabel屬性*/-(void) updateTimer{ //1,取出標籤中得數字 int counter = self.counterLabel.text.intValue; //2,判斷是否為0,如果是則停止時鐘 if (--counter<0) { //提示使用者,提示框 [[[UIAlertView alloc] initWithTitle:@"開始" message:@"開始啦。。。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil] show]; //AlertView 中輸入的最後是數組,可以通過代理方式來實現方法 [self pause]; }else{ //,3修改數字並顯示更新UILabel self.counterLabel.text = [NSString stringWithFormat:@"%d",counter]; }}/**暫停*/-(IBAction)pause{ //停止時鐘,invalidate是唯一的方法,一調用就幹掉timer了,想再用只能重新執行個體化 [self.timer invalidate];}-(IBAction)stop{ [self pause]; self.counterLabel.text = @"10";}#pragma mark - alertView 代理方法-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ NSLog(@"%d",buttonIndex);}@end
注意點NSTimer
用法:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]
參數說明:
1,時間間隔,double
2,監聽時鐘觸發的對象
3,調用方法
4,userInfo,可以是任意對象,通常傳遞nil,如果有傳遞值,大多數是為了在多個計數器中分辨用哪個
5,repeats:是否重複執行調用方法。
是否要在發生滾動事件時候繼續計時器
將timer添加到運行迴圈,模式:NSRunLoopCommonModes監聽滾動模式
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
提示框 UIAlertView
提示框
[[[UIAlertView alloc] initWithTitle:@”開始” message:@”開始啦。。。” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”確定”, nil] show];
AlertView 中輸入的最後是數組,可以通過代理方式來實現方法
#pragma mark - alertView 代理方法-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ NSLog(@"%d",buttonIndex); //0指的是取消按鈕 //可以加入if判斷buttonIndx為多少來加入事件}
ps:建立iOS交流學習群:304570962
可以加貓貓QQ:1764541256 或則znycat
讓我們一起努力學習吧。
原文:http://blog.csdn.net/u013357243?viewmode=contents