NSRunLoop&NSTimer混合使用

牧云@^-^@ 提交于 2020-03-01 17:15:43

//

//  ViewController.m

//  NSRunLoop

//

//  Created by JackMeng on 13-11-14.

//  Copyright (c) 2013 JackMeng. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //使用NSTimer创建定时器

    NSTimeInterval timeInterval = 1;//间隔时间

    [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)timerMethod{

    

    NSLog(@"我是定时器one");

}

- (void)timerMethod2{

    

    NSLog(@"我是定时器two");

}

- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!