How to prevent 2 Animation on Object at the same time in Objective-C?

42 views Asked by At

I have a UILabel and each time it touched, it'll scaled with CGAffineTransformMakeScale and animation, then back to real size with animation. Now if it touched before last animation ends, it'll scale the scaled object not the real size of main object and so it'll scaled more than it should to be scale. How to prevent this problem ? I want to second touch before last scale ending scale the main size of object.

Here is My Code :

(void)MyLabelTouched
{
[UIView animateWithDuration:.15 animations:^{
            MyLabel.transform = CGAffineTransformMakeScale(1.5,1.5);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:.15 animations:^{
                MyLabel.transform = CGAffineTransformIdentity;
            }];
        }];
}
1

There are 1 answers

1
Shehata Gamal On

Try this

(void)MyLabelTouched
{ 
   if(animRunning){return;}
   animRunning = YES;
   [ MyLabel.layer removeAllAnimations];
    MyLabel.transform = CGAffineTransformIdentity;
   [UIView animateWithDuration:.15 animations:^{
        MyLabel.transform = CGAffineTransformMakeScale(1.5,1.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.15 animations:^{
            MyLabel.transform = CGAffineTransformIdentity;
            animRunning = NO;
        }];
    }];
 }