iPhone开发之NSRunLoop简单使用

//
//  ViewController.m
//  RunLoopDemo
//
//  Created by Fox on 12-5-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    //使用NSTimer创建定时器
    NSTimeInterval timeInterval = 2;//间隔时间
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
    
    
    //使用RunLoop创建NSTimer对象
    NSRunLoop *theRunLoop = [NSRunLoop currentRunLoop];//获得当前的RunLoop
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0];//创建对象,指定首次启动的时间
    NSTimer *theTimer = [[NSTimer alloc] initWithFireDate:fireDate interval:2 target:self selector:@selector(timerMethod2) userInfo:nil repeats:YES];
    [theRunLoop addTimer:theTimer forMode:NSDefaultRunLoopMode];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)timerMethod{

    NSLog(@"NSTimer1执行");
}
- (void)timerMethod2{

     NSLog(@"NSTimer2执行");
}


@end

  

原文地址:https://www.cnblogs.com/foxmin/p/2498122.html