HurkNburkS
HurkNburkS

Reputation: 5510

Make uibutton pulsate.

I am trying to make a uibutton that has an image in it pulsate slowly by changing the alpha level back and forth once the view loads...

currently I am doing this but it does nothing....

-(void)loopdyloop
{
    while ( myCount < 1000 )
    {

        [UIView animateWithDuration:3.0
                              delay:0.0f
                            options:UIViewAnimationCurveEaseOut
                         animations:^{


                             iconButton.alpha = 0.0;


                         } completion:^(BOOL finished) {
                             if (finished) {

                                 NSLog(@"animated");
                             }
                         }];


        [UIView animateWithDuration:3.0
                              delay:0.0f
                            options:UIViewAnimationCurveEaseOut
                         animations:^{


                             iconButton.alpha = 1.0;


                         } completion:^(BOOL finished) {
                             if (finished) {

                                 NSLog(@"animated");
                             }
                         }];




        myCount++;
    }

}

this method is called from the viewdidload method,

pretty crude I know but its my best attempt, if you have any idea on how I could achieve this it would be greatly appreciated.

Upvotes: 1

Views: 2397

Answers (2)

Bazinga
Bazinga

Reputation: 2466

How about applying this in your button layer in viewDidLoad:

CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = FLT_MAX;
[YOURButton.layer addAnimation:pulseAnimation forKey:nil];

and you can experiment with it.

Upvotes: 14

David R&#246;nnqvist
David R&#246;nnqvist

Reputation: 56625

If you want it to repeat forever you could add the animation options to auto reverse and repeat, like this:

[UIView animateWithDuration:3.0
                      delay:0.0f
                    options:UIViewAnimationCurveEaseOut | 
                            UIViewAnimationOptionRepeat | 
                            UIViewAnimationOptionAutoreverse
                 animations:^{
                     iconButton.alpha = 0.0;
                 } 
                 completion:^(BOOL finished) {
                     // You could do something here
                 }];

It will cause the alpha to first animate to 0.0, then back to the original (1.0) and then do those two things over and over and over...

Upvotes: 5

Related Questions