1, delayed execution (non-repeatable)
[OBJC] View plain copy /* * * * Delay cannot be duplicated * /-(void) timermethoda { [self Performselector: @selector (methodaevent) withobject:nil afterdelay:2.0f]; // Delay Time } -(void) methodaevent { NSLog (@ "--method_a "); }
Effect I directly intercept the console log, do not do the UI.
2, use Nstimer to perform timing and delay (repeatable)
[OBJC] View plain copy/** * * Timer can be repeated **/- (void) Timermethodb {_timer= [Nstimer scheduledtimerwithtimeinterval:1.0f //time intervaltarget:self selector: @selec Tor (methodbevnet) Userinfo:nil Repeats:yes]; } - (void) methodbevnet {count++; NSLog (@"--method_b count:%d", Count); if(Count >=5) {[_timer invalidate]; //Repeat 5 times, timer stopsNSLog (@"--End"); } }
3, perform timing with dispatch_source_set_timer (repeatable)
[OBJC] View plain copy/** * * * Dispatch_time can be repeated **/- (void) timermethodc {__blockinti =0; CGFloat Duration=1.0f;//time intervaldispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); dispatch_source_t Timer= Dispatch_source_create (Dispatch_source_type_timer,0,0, queue); Dispatch_source_set_timer (Timer, Dispatch_time_now, duration* Nsec_per_sec,0); Dispatch_source_set_event_handler (Timer,^{i++; if(I >5) {dispatch_source_cancel (timer); //stop after 5 executionsNSLog (@"--End"); }Else{NSLog (@"--Method_c i:%d", i); } }); Dispatch_resume (timer); }
4, execution delay with dispatch_after (non-repeatable)
[OBJC] View plain copy /* * * * * dispatch_time not repeatable ** /-(void) timermethodd { 2.0f; // delay Time dispatch_time_t timer = Dispatch_time (Dispatch_time_now, (int64_t) (Delaytime *nsec_per_sec)); ^{ NSLog (@ "--method_d time:%@", [nsdate Date]); }
The article concludes with a demo
http://download.csdn.net/detail/xiongbaoxr/9417374
Four easy-to-use demos, about iOS timers and latency, are very handy.