ios - Ignore UIButton's touch event in a long task time -


i want ignore touch events of button while long task running.

- (void)buttonaction{     nslog(@"click!");     button.enabled = no;     [self longtask]; }  - (void)longtask{     nslog(@"task begin!");     sleep(5);     nslog(@"task finished!");     button.enabled = yes; } 

during longtask time, click button again, nothing happens. but, when longtask finished, automatically respond click events , execute longtask again! how many times clicked when button's enabled value 'no', longtask perform how many times.

2013-08-20 09:24:49.478 appname[2518:c07] click! 2013-08-20 09:24:49.479 appname[2518:c07] task begin! 2013-08-20 09:24:54.481 appname[2518:c07] task finished! 2013-08-20 09:24:54.482 appname[2518:c07] click! 2013-08-20 09:24:54.482 appname[2518:c07] task begin! 2013-08-20 09:24:59.484 appname[2518:c07] task finished! 

i tried set userinteractionenabled=no got same result.

how can make ignores touch events when long task running , never performs task? in other words, executes longtask when button clicked @ it's enabled value 'yes'?

thanks help!

the sleep freezing main thread, responsible ui interations.

you should perform long tasks in background, can easlily achieved gcd. below , should able achieve want:

- (void)buttonaction{      nslog(@"click!");     button.enabled = no;      dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^(void){         //background thread          [self longtask];          dispatch_async(dispatch_get_main_queue(), ^(void){              button.enabled = yes;         });     }); }  - (void)longtask{     nslog(@"task begin!");     [nsthread sleepfortimeinterval:5];     nslog(@"task finished!");  }  

notice when way, ui won't blocked anymore, desired button disabled.

as @erik godard mentioned, should consider use kind of ui feedback when performing kind of tasks. can start process indicator in same area set button's enabled property no , stop when setting property yes

another way accomplish this, without gcd, changing sleep nsrunloop's method rununtildate. in way, main thread won't blocked too, , able achieve wanting.

- (void)buttonaction{      nslog(@"click!");     self.addcartbutton.enabled = no;    [self longtask]; }  - (void)longtask{     nslog(@"task begin!");     [[nsrunloop currentrunloop] rununtildate:[nsdate datewithtimeintervalsincenow:5]];     nslog(@"task finished!");    self.addcartbutton.enabled = yes; }  

both approach tested , seems working.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -