iOS——NSTimer的使用,计时器的简单创建
2016-09-19 08:47
267 查看
NSTimer用到的地方很多,这里仅以计时器作为例子,里面的代码可以应用在很多程序中。
以下是全部的源码。
效果图如下:
以下是全部的源码。
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSTimer *timer; @property (weak, nonatomic) IBOutlet UILabel *timerLabel; @property (nonatomic, assign) int count; @end @implementation ViewController - (IBAction)startBtnClick:(id)sender { //开始</span> if (self.timer) { [self.timer invalidate]; self.timer = nil; } self.count = 0; self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(repeatShowTime:) userInfo:@"admin" repeats:YES]; } - (IBAction)stopBtnClick:(id)sender { //停止</span> if (self.timer) { [self.timer invalidate]; self.timer = nil; } self.count = 0; self.timerLabel.text = @"00:00"; } - (IBAction)pauseBtnClick:(id)sender { //暂停</span> [self.timer setFireDate:[NSDate distantFuture]]; } - (IBAction)continueClick:(id)sender { //继续</span> [self.timer setFireDate:[NSDate date]]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)repeatShowTime:(NSTimer *)tempTimer { self.count++; self.timerLabel.text = [NSString stringWithFormat:@"%02d:%02d",self.count/60,self.count%60]; } - (void)dealloc { //销毁NSTimer</span> if (self.timer) { [self.timer invalidate]; self.timer = nil; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
效果图如下:
相关文章推荐
- iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- 【iOS开发】简单UI部分UIButton的创建和UITextField的使用
- iOS 简单的计时器NSTimer
- iOS计时器NSTimer的使用
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- 在iOS应用中使用UIWebView创建简单的网页浏览器界面
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
- iOS个人整理01-创建UI工程及UIView简单使用
- Parse教程——如何简单地使用Parse在iOS应用创建后台服务
- iOS雨笙 NSTimer(计时器)和NSUserDefaults(最轻量级的持久化)的简单应用
- IOS中计时器NSTimer的使用
- iOS XIB文件的简单创建使用
- Parse教程——如何简单地使用Parse在iOS应用创建后台服务
- iOS-UIImage的创建和简单使用
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- IOS_简单创建tableView并使用自定义的单元格
- iOS开发中SQLite简单使用(基础用法:创建表,增、删、改、查)
- NSTimer的两种创建方式以及跟RunLoop的简单结合使用
- iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- IOS sqlite简单使用 以及为什么sqlite3_open无法创建数据库的原因