1

I want to change the text of my UILabel with a Blink animation. The text should be "text1" with black color, disappear and then change to "text2" and red color and viceversa. Here is my code

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self performSelector:@selector(combineAnimations) withObject:self afterDelay:0.0];
    self.label.textColor = [UIColor blackColor];
    self.label.text = @"text1";

    [self animateView];
}

- (void) animateView {
    [UIView animateWithDuration:2.0
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.label.text = @"text2";
                         self.label.textColor = [UIColor redColor];
                     }
                     completion:nil];
}

What am I missing?

2
  • What visual effects are produced by your current code?
    – rob mayoff
    Jun 26, 2014 at 20:21
  • The second text overlaps the previous one for a while and then changes back again with the same effect.
    – nicky_1525
    Jun 26, 2014 at 20:22

1 Answer 1

-1

Use two methods that call each other, in the completion block. You can also adjust a quick fade in or out...which is much less jarring than a simple "blink". If you want the blink, with no time between, just set the delay to 0.0. Similarly, you can adjust the alpha in there to get a fade effect.

- (void) animateView {

self.label.text = @"text2";
                     self.label.textColor = [UIColor redColor];
[UIView animateWithDuration:.5
                      delay:1.0
                    option:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.label.alpha = 1;
                 }
                 completion:{
      [self animateViewReverse];
}];
}

- (void) animateViewReverse {

 self.label.text = @"text1";
 self.label.textColor = [UIColor blackColor];
[UIView animateWithDuration:1.0
                      delay:1.0
                    option:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.label.alpha = 1;

                 }
                 completion:{
          [self animateView];
}];
}

One thing you seem to be a bit confused about is animating the label's text. You don't actually animate the text string, just things like the alpha, size, position. So, in your case you can change the color and text string BEFORE animating, just use the animation as a way to trigger the next animation/reverse - it also builds in the functionality for more nuanced animations (besides a simple blink) later on.

In you wanted to do this without animations at all, you could simply create two methods, each setting the text one way, then calling the other on a delay like:

[self performSelector:@selector(animationReverse) withObject:nil afterDelay:2];
1
  • What about it works or doesn't work? If you'd give some specifics, I am sure we can get you what you need.
    – Fluffhead
    Jun 27, 2014 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.