瞭解NSTimer

來源:互聯網
上載者:User

NSTimer

 

建立一個 Timer

     
+ scheduledTimerWithTimeInterval: invocation: repeats:

      + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

     
+ scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:

         
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

         
建立返回一個新的NSTimer對象和時間表,在當前的預設模式下迴圈調用一個執行個體方法。

     
+ timerWithTimeInterval: invocation: repeats:

      + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

     
+ timerWithTimeInterval: target:selector: userInfo:repeats:

         
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

     
– initWithFireDate: interval: target: selector: userInfo: repeats:

         
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;

scheduledTimerWithTimeInterval:(NSTimeInterval)seconds

預訂一個Timer,設定一個時間間隔。

表示輸入一個時間間隔對象,以秒為單位,一個>0的浮點類型的值,如果該值<0,系統會預設為0.1

target:(id)aTarget

表示發送的對象,如self

selector:(SEL)aSelector

方法選取器,在時間間隔內,選擇調用一個執行個體方法

userInfo:(id)userInfo

此參數可以為nil,當定時器失效時,由你指定的對象保留和釋放該定時器。

repeats:(BOOL)yesOrNo

當YES時,定時器會不斷迴圈直至失效或被釋放,當NO時,定時器會迴圈發送一次就失效。

invocation:(NSInvocation *)invocation

啟動 Timer

     
– fire

停止 Timer

     
– invalidate

Timer設定

     
– isValid

     
– fireDate

     
– setFireDate:

     
– timeInterval

     
– userInfo

NSTimeInterval類:是一個浮點數字,用來定義秒

例子:

iphone為我們提供了一個很強大得時間定時器NSTimer

他可以完成任何定時功能:

我們使用起來也很簡單,只要記住三要素就可以,具體得三要素是:時間間隔NSTimeInterval浮點型,事件代理

delegate和事件處理方法@selector();就可以用

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
來初始化一個時間定時器

下面我寫了一個很簡單得例子

初始化一個定時器:

-(void)initTimer

{

//時間間隔

NSTimeInterval timeInterval =1.0 ;

//定時器

NSTimer showTimer = [NSTimer scheduledTimerWithTimeInterval:maxShowTime

target:self

selector:@selector(handleMaxShowTimer:)

userInfo:nil

repeats:NO];

}

//觸發事件

-(void)handleMaxShowTimer:(NSTimer *)theTimer

{

NSDateFormatter dateFormator = [[NSDateFormatter alloc] init];

dateFormator.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSString *date = [dateformater stringFromDate:[NSDate date]];

if([date isEqualToString:@"2011-11-09 23:59:59"])

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME

 message:@"現在馬上就有新的一天了!"

 delegate:self

 ancelButtonTitle:nil

 otherButtonTitles:CONFIRM_TITLE, nil];

[alert show];

[alert release];

}

[data release];

[dateFormator release];

}

另外附一個例子:方框賽跑

-(void)viewDidLoad

{

 

[super viewDidLoad];

CGRect workingFrame;

 

workingFrame.origin.x = 15;

workingFrame.origin.y = 400;

workingFrame.size.width = 40;

workingFrame.size.height = 40;

 

for(int i = 0; i < 6; i++)

{

UIView *myView = [[UIView alloc] initWithFrame:workingFrame];

[myView setTag:i];//標記方塊

[myView setBackgroundColor:[UIColor blueColor]];

 

workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;

[self.view addSubview:myView];

}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.