Pulsing Animation
Asked Answered
U

3

5

I want to build a pulsing animation on a simple UIImageView. The ImageView will grow a little bit bigger, then go back to its original size.

I used the following code:

- (void) doCoolAnimation {
    [UIView beginAnimations:@"glowingAnimation" context:nil];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:INT_MAX];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationBeginsFromCurrentState:YES];
    imageView.transform = CGAffineTransformMakeScale(1.15, 1.15);
    [UIView commitAnimations];
}

This works fine on iOS3 but works only partially on iOS4.

I have a UITabBarController with 2 views in it. In the first one is the imageView with the animation, and the animation starts as soon as the view is loaded. But after I switch to the second view (using TabBar) and back, the animation is not running anymore on iOS4. (But on iOS3 I can switch between these 2 views and the animation still works fine.)

I also tried with a timer that calls doCoolAnimation every second, but that does not help to start the animation again.

Can someone explain why after view switching the animation is gone? Is there a workaround that can make it work on iOS4?

Upshaw answered 6/9, 2010 at 16:52 Comment(0)
A
9

Use this simple method :-

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;
[ButtonName.layer addAnimation:pulseAnimation forKey:nil];
Angeloangelology answered 30/7, 2012 at 8:49 Comment(1)
It is really cool! Also is there anyway to check whether any animation is already going on?Onions
V
1

ViewDidLoad is only called the first time the view loads. Since the view is not deallocated immediately when you switch views, as in it still exists, viewdidLoad is not called again when you come back into the view.

Try calling [self doCoolAnimation]; in viewDidAppear. This is called every time.

- (void)viewDidAppear:(BOOL)animated {
    [self doCoolAnimation]
}
Valency answered 8/12, 2010 at 17:13 Comment(0)
M
0

Swift 5 version:

    let pulseAnimation = CABasicAnimation(keyPath: "transform.scale")
    pulseAnimation.duration = 0.5
    pulseAnimation.toValue = NSNumber(value: 1.1)
    pulseAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
    pulseAnimation.autoreverses = true
    pulseAnimation.repeatCount = .greatestFiniteMagnitude
    coinImageView.layer.add(pulseAnimation, forKey: nil)
Mantissa answered 2/12, 2022 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.