貓貓學IOS(十)UI之_NSTimer_ios計時器,ios_nstimer_ios

來源:互聯網
上載者:User

貓貓學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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.