猫猫学IOS(十)UI之_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

原文地址:https://www.cnblogs.com/znycat/p/4375200.html