ios - How to reliably block duplicate animation calls in Objective-C? -
a user can initiate animation swipe gesture. want block duplicate calls animation, make sure once animation has started, cannot initiated again until has completed -- may happen if user accidentally swipes multiple times.
i imagine people achieve control using boolean flag (bool isanimatingflag) in manner shown @ bottom. i've done things before in apps many times -- never feel 100% whether flag guaranteed have value intend, since animation uses blocks , it's unclear me thread animation completion block being run on.
is way (of blocking duplicate animations) reliable multi-thread execution?
/* 'atomic' doesn't * guarantee thread safety * i've set flag follows: * correct intended usage? */ @property (assign, nonatomic) bool isanimatingflag; //… @synthesize isanimatingflag //… -(void)starttheanimation{ // (1) return if isanimatingflag true if(self.isanimatingflag == yes)return; /* (2) set isanimatingflag true * intention prevent duplicate animations * may caused unwanted double-tap */ self.etiisanimating = yes; // (3) start new animation [uiview animatewithduration:0.75 delay:0.0 options:nil animations:^{ // animations happen here... } completion:^(bool finished) { // (4) reset flag enable further animations self.isanimatingflag = no; }]; }
disable gesture if don't want user triggering multiple times
- (void)starttheanimation:(id)sender { [sender setenabled:no]; [uiview animatewithduration:0.75 delay:0.0 options:nil animations:^{ // animations happen here... } completion:^(bool finished) { [sender setenabled:yes]; }]; } update
gestures have enabled property use same idea if button , change it' enabled state
Comments
Post a Comment