All codes are as follows: is a random change of the three-base color of an artboard
#import "AppDelegate.h" @interface AppDelegate () { nstimer *_timer;/ /timer} @end @implementation appdelegate- (void) dealloc { self.window = Nil; [super dealloc];} - (void) creatbutton { nsarray *arr = @[@ "start" @ "Pause"]; for (nsinteger i = 0; i < arr.count; i++) { uibutton *button = [ uibutton buttonwithtype:uibuttontypesystem]; //coordinates button.frame = cgrectmake ([UIScreen mainScreen]. BOUNDS.SIZE.WIDTH/2-50,&NBSP;100+I*60,&NBSP;100,&NBSP;50); // Set Title [button setTitle:arr[i] forstate:uicontrolstatenormal]; //Add Event self execute btnclick: //with parameters Who is increasing the event then the arguments are passed on who [button addtarget:self action: @selector (Btnclick:) forControlEvents:UIControlEventTouchUpInside]; //set tag value-sensitive button button.tag = 1+i; //paste [self.window addSubview:button]; }}-(void) Btnclick: (uibutton *) btn{ switch (Btn.tag) { case 1: { //Start Timer [_timer setFireDate:[NSDate distantPast]]; } break; case 2://Timer Pause { [_timer setfiredate:[ nsdate distantfuture]]; } break; default: break; } }//button the method that is called after the trigger- (void) Creattimer { /* first parameter: Time interval units s 2 : target Object address (any) general write self &NBSP;&NBSP;3&NBSP;&NBSP;&NBSP;&NBSP;&NBSP: Behavior of the target object selector 4 : nil 5 : YES Repeat Once you create a timer with this function then the timer will start immediately After the timer starts a sub-thread each time notifies the ui main thread makes self call timerClick method */ //instantiation of a timer object _timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector ( Timerclick) userinfo:nil repeats:yes]; //is created by default NSTimer will start after //but we need Click the button to start //Now we should pause the timer first //Pause Timer [_timer setFireDate:[NSDate distantFuture]]; }//Timer triggered function- (void) timerclick { // NSLog (@ "%s", __func__);// // cgfloat red = Arc4random ()%256/255.0;//randomly generates 0--1// cgfloat green = arc4random ()% 256/255.0;// cgfloat blue = arc4random ()%256/255.0;// //color matching with three primary colors //0-1 self.window.backgroundcolor = [uicolor colorwithred:arc4random ()%256/255.0 green: Arc4random ()%256/255.0 blue:arc4random ()%256/255.0 alpha:1];} - (BOOL) Application: (uiapplication *) APPLICATION&NBSP;DIDFInishlaunchingwithoptions: (nsdictionary *) launchoptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; [self creatbutton]; //Creating Timers [self creatTimer]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makekeyandvisible]; return yes;} - (void) applicationwillresignactive: (uiapplication *) application { // sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such As an incoming phone call or sms message) or when the user quits the application and it begins the transition to The background state. // use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES Frame rates. games should use this method to pause the game.} - (void) Applicationdidenterbackground: (uiapplication *) application { // use this method to release shared resources, save user data , invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // if your application supports background execution, this method is called instead of Applicationwillterminate: when the user quits.} - (void) Applicationwillenterforeground: (uiapplication *) application { // called as part of the transition from the background to the inactive state; here you can undo many of the Changes made on entering the background.} - (void) applicationdidbecomeactive: (uiapplication *) application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in The background, optionally refresh the user interface.} - (void) Applicationwillterminate: (uiapplication *) application { // Called when the application is about to terminate. save data if appropriate. see also applicationdidenterbackground:.} @end
IOS Nstimer Sample Demo