Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView two animations coexisting

How can I have one animation continue forever after calling a second animation? For example:

1) start an object pulsating 2) Move it while its pulsating 3) it continues pulsating

Everything works except the second animation is stopping the first one indefinitely. Below is some sample code:

//Pulsate **

        [UIView animateWithDuration:0.25
                              delay:0
                            options: (UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat)
                         animations:^{
                             CGAffineTransform currentTransform = self.transform;
                             CGAffineTransform newTransform1 = CGAffineTransformScale(currentTransform, .95, .95);
                             [self setTransform:newTransform1];
                             CGAffineTransform newTransform2 = CGAffineTransformScale(currentTransform, 1, 1);
                             [self setTransform:newTransform2];
                         } 
                         completion:nil];    


//Move **
     [UIView animateWithDuration:0.30
                      delay:0
                    options: (UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState )
                 animations:^{
                     [[(UIPinchGestureRecognizer*)sender view] setCenter:CGPointMake(myAppDelegate.MCViewReference.center.x-300, myAppDelegate.MCViewReference.center.y)];
                 } 
                 completion:^(BOOL finished){
                }];    
like image 731
user973984 Avatar asked Mar 08 '26 19:03

user973984


1 Answers

You will not be able to do this with the block-based animations as you have them here. You will need to split your animations up using explicit animations with CABasicAnimation. Create one animation for the pulsating effect and set it to repeat indefinitely. Then you can move it around by setting the center (either animated or non-animated).

CABasicAnimation *pulsation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulsation.fromValue = [NSNumber numberWithFloat:0.95f];
pulsation.toValue = [NSNumber numberWithFloat:1.f];
pulsation.duration = 0.25f;
pulsation.autoreverses = YES;
pulsation.repeatCount = INFINITY;

[self.layer addAnimation:pulsation forKey:@"pulse"];

As soon as you add the animation to the layer, it will begin animating. To remove the animation, simply call [self.layer removeAnimationForKey:@"pulse" or removeAllAnimations:.

like image 147
larsacus Avatar answered Mar 11 '26 07:03

larsacus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!