67cherries
67cherries

Reputation: 6951

How to animate UILabel Scale Repeatedly

I'm working on a new app and I wanted to do a tap to continue opening screen. What I'm doing is scaling a UILabel (created in IB) up and down like this:

note:self.labelShouldScaleUp is a BOOL.

-(void)animateLabel{
if(self.labelShouldScaleUp){
    [UIView animateWithDuration:1 animations:^(void){

        self.clickToContinueLabel.transform=CGAffineTransformScale(self.clickToContinueLabel.transform, 2, 2);
    } completion:^(BOOL finished){
        self.labelShouldScaleUp=NO;
    }];
}
if(!self.labelShouldScaleUp){
    [UIView animateWithDuration:1 animations:^(void){

        self.clickToContinueLabel.transform=CGAffineTransformScale(self.clickToContinueLabel.transform, 0.5, 0.5);
    } completion:^(BOOL finished){
        self.labelShouldScaleUp=YES;
    }];
}
[self performSelector:@selector(animateLabel) withObject:nil afterDelay:1.05];

}

The code works, but when the label is done scaling every time, it jumps to the right or left and then scales the other way. I'm not sure why this is happening.

Thanks in advance for you help.

Upvotes: 0

Views: 3129

Answers (1)

Tommie C.
Tommie C.

Reputation: 13181

I think you should take an extended look at the UIView animation options. Here is a sample. You may want to use one of UIViewAnimationOptions. There is another SO Question on this topic. You can also review the methods in the Animating Views with Block Objects section of the Apple docs reference page

-(void)animateDisplayText
{
    [self.view bringSubviewToFront:self.happyMessageLabel];
    self.happyMessageLabel.alpha = 0.0f;
    self.happyMessageLabel.textColor = [UIColor yellowColor];
    self.happyMessageLabel.shadowColor = [UIColor redColor];
    self.happyMessageLabel.text = [self.controller.data randomHappyMessage];
    self.happyMessageLabel
    .font = kFontHappyMessageLabel;

    // set font size which you want instead of 35
    self.happyMessageLabel.transform = CGAffineTransformScale(self.happyMessageLabel.transform, 0.25, 0.25);

    [UIView animateWithDuration:1.5 delay: 0.0 options:UIViewAnimationOptionTransitionFlipFromBottom
                     animations:^{

        self.happyMessageLabel.alpha = 1.0f;

        self.happyMessageLabel.transform = CGAffineTransformScale(self.happyMessageLabel.transform, 0.35, 0.35);
        self.happyMessageLabel.transform = CGAffineTransformScale(self.happyMessageLabel.transform, 5, 5);



    } completion:^(BOOL finished){

        [UIView animateWithDuration:2.5 animations:^{


            self.happyMessageLabel.transform = CGAffineTransformScale(self.happyMessageLabel.transform, 5, 5);
            self.happyMessageLabel.transform = CGAffineTransformScale(self.happyMessageLabel.transform, 0.35, 0.35);

            self.happyMessageLabel.alpha = 0.3f;
            [self.view sendSubviewToBack:self.happyMessageLabel];
        }];


    }];

    self.happyMessageLabel.transform = CGAffineTransformIdentity;

}

Upvotes: 2

Related Questions