iOS学习笔记 (5)NSDate和NSTimer
2015-07-02 20:29
411 查看
NSDate类的使用
//1、创建日期对象
NSDate *date1=[[NSDate
alloc]init];
NSLog(@"%@",date1);//默认格林尼治时间相差8个时区
//2、
NSDate *date2=[NSDate
date];
//返回一个从1970-01-01 00:00:00
后一段时间的时间
date2=[NSDate
dateWithTimeIntervalSince1970:10];
//返回当前时间一段时间后的时间
NSDate *date3=[NSDate
dateWithTimeIntervalSinceNow:3600*8];
NSLog(@"%@",date2);
//相对于某个日期后的什么时间
NSDate *date4=[NSDate
dateWithTimeInterval:3600
sinceDate:date2];
NSLog(@"%@",date4);
//返回两个时间点较早的时间
[date4
earlierDate:date3];
//返回两个时间点比较晚的时间
[date3
laterDate:date2];
//比较两个时间大小
[date3
compare:date2];
//判断两个时间是否相等
[date3
isEqualToDate:date2];
//返回一个很久很久的时间
NSDate *date6= [NSDate
distantPast];
NSLog(@"%@",date6);
//返回一个未来的时间
NSDate *date7=[NSDate
distantFuture];
// NSLog(@"%@",date7);
//将一个定时器(NSTimer)的开启时间设置为很早以前的时间会启动定时器
//设置成为一个未来的时间的一个时间,会关闭定时器
NSTimeInterval sub=[date1
timeIntervalSinceDate:date2];
NSLog(@"%f",sub);
//计算当前时间与系统时间的差值
NSTimeInterval sub2=[date6
timeIntervalSinceNow];
NSLog(@"%.2f",sub2);
NSTimer类的使用
@implementation RootViewController
{
NSTimer *timer;//定时器
}
-(void)dealloc
{
//释放timer
if(!timer)
{
[timer
invalidate];
}
[super
dealloc];
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
[self
createButton];
[self
createLable];
[self
createTimer];
}
#pragma mark-- 创建button
-(void)createButton
{
UIButton *button=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
[button
setFrame:CGRectMake(100,
50, 100,
50)];
button.backgroundColor=[UIColor
redColor];
[button setTitle:@"开始"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(onClick:)
forControlEvents:UIControlEventTouchDown];
[self.view
addSubview:button];
[button
release];
}
-(void)onClick:(UIButton *)button
{
static BOOL isRuning=NO;
if(isRuning){
//启动计时器,将时间设置为过去的一个时间
[timer
setFireDate:[NSDate
distantPast]];
[button setTitle:@"停止"
forState:UIControlStateNormal];
isRuning=NO;
}else{
//暂停定时器,将时间设置为未来的一个时间
[timer
setFireDate:[NSDate
distantFuture]];
[button setTitle:@"开始"
forState:UIControlStateNormal];
isRuning=YES;
}
}
#pragma mark 创建timer
-(void)createTimer
{
//第一个参数是timer得间隔时间,就是说我的timer多少时间启动一次
//第二个参数是接受消息的的对象
//第三个参数是方法选择器。就是多少时间调用一次该方法
//第四个参数是用户信息,比如error
//第五个参数是否重复执行
//定时器是开始即启动
timer=[NSTimer
scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(timeRun)
userInfo:nil
repeats:YES];
//先暂停,将时间设置为未来的一个时间
[timer
setFireDate:[NSDate
distantFuture]];
}
-(void)timeRun
{
static int count=1;
NSLog(@"定时器第%d次调用!",count);
count++;
}
//1、创建日期对象
NSDate *date1=[[NSDate
alloc]init];
NSLog(@"%@",date1);//默认格林尼治时间相差8个时区
//2、
NSDate *date2=[NSDate
date];
//返回一个从1970-01-01 00:00:00
后一段时间的时间
date2=[NSDate
dateWithTimeIntervalSince1970:10];
//返回当前时间一段时间后的时间
NSDate *date3=[NSDate
dateWithTimeIntervalSinceNow:3600*8];
NSLog(@"%@",date2);
//相对于某个日期后的什么时间
NSDate *date4=[NSDate
dateWithTimeInterval:3600
sinceDate:date2];
NSLog(@"%@",date4);
//返回两个时间点较早的时间
[date4
earlierDate:date3];
//返回两个时间点比较晚的时间
[date3
laterDate:date2];
//比较两个时间大小
[date3
compare:date2];
//判断两个时间是否相等
[date3
isEqualToDate:date2];
//返回一个很久很久的时间
NSDate *date6= [NSDate
distantPast];
NSLog(@"%@",date6);
//返回一个未来的时间
NSDate *date7=[NSDate
distantFuture];
// NSLog(@"%@",date7);
//将一个定时器(NSTimer)的开启时间设置为很早以前的时间会启动定时器
//设置成为一个未来的时间的一个时间,会关闭定时器
NSTimeInterval sub=[date1
timeIntervalSinceDate:date2];
NSLog(@"%f",sub);
//计算当前时间与系统时间的差值
NSTimeInterval sub2=[date6
timeIntervalSinceNow];
NSLog(@"%.2f",sub2);
NSTimer类的使用
@implementation RootViewController
{
NSTimer *timer;//定时器
}
-(void)dealloc
{
//释放timer
if(!timer)
{
[timer
invalidate];
}
[super
dealloc];
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
[self
createButton];
[self
createLable];
[self
createTimer];
}
#pragma mark-- 创建button
-(void)createButton
{
UIButton *button=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
[button
setFrame:CGRectMake(100,
50, 100,
50)];
button.backgroundColor=[UIColor
redColor];
[button setTitle:@"开始"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(onClick:)
forControlEvents:UIControlEventTouchDown];
[self.view
addSubview:button];
[button
release];
}
-(void)onClick:(UIButton *)button
{
static BOOL isRuning=NO;
if(isRuning){
//启动计时器,将时间设置为过去的一个时间
[timer
setFireDate:[NSDate
distantPast]];
[button setTitle:@"停止"
forState:UIControlStateNormal];
isRuning=NO;
}else{
//暂停定时器,将时间设置为未来的一个时间
[timer
setFireDate:[NSDate
distantFuture]];
[button setTitle:@"开始"
forState:UIControlStateNormal];
isRuning=YES;
}
}
#pragma mark 创建timer
-(void)createTimer
{
//第一个参数是timer得间隔时间,就是说我的timer多少时间启动一次
//第二个参数是接受消息的的对象
//第三个参数是方法选择器。就是多少时间调用一次该方法
//第四个参数是用户信息,比如error
//第五个参数是否重复执行
//定时器是开始即启动
timer=[NSTimer
scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(timeRun)
userInfo:nil
repeats:YES];
//先暂停,将时间设置为未来的一个时间
[timer
setFireDate:[NSDate
distantFuture]];
}
-(void)timeRun
{
static int count=1;
NSLog(@"定时器第%d次调用!",count);
count++;
}
相关文章推荐
- 使用LeanClound(AVOS)造成的ios linker command failed with exit code 1 (use -v to see invocation)
- IOS页面自动布局 之 NSLayoutConstraint基础篇
- iOS9适配系列教程
- iOS9适配系列教程
- iOS 详解CoreLocation定位服务
- IOS 百度地图一直报 manager start failed!
- IOS 8.1.3 GameCenter code15 沙盒无法登陆
- 解决Xcode升级导致插件不管用的问题
- iOS---设备的唯一标识详解
- 我为什么要学IOS?
- iOS8 高德地图SDK MAMapView无法定位的问题
- JS与iOS Native Code互调的优雅实现方案
- CocoaPods的安装和使用方法
- iOS开发多线程篇—NSOperation简单介绍
- IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)
- iOS 生产环境之实时日志,都打包了怎么查看报错信息
- 【转】storyboard入门
- 【转】在iOS 7中使用storyboard
- IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用