How to wait for CAAnimation to repeat to complete the loop before deleting it

I have an idea that I'm throbbing with CAAnimation.

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
animation.values = @[ @0.0f, @1.0f, @0.0f ];
animation.duration = 0.5;
animation.repeatCount = HUGE_VALF;

[view.layer addAnimation:animation forKey:@"pulsate"];

When I remove the animation with [view.layer removeAnimationForKey:@"pulsate"], the opacity instantly disappears. What I would like to achieve is that the currently pulsating animation is completed, and then the animation is deleted.

I tried setting repeatCountto 1, but this throws an exception because the animation is immutable.

I also tried to get the current value from the presentation level and apply it to the model, and then remove the animation and add the animation again to complete it. But this gives noticeable hiccups when the animation stops, and also usually turns off from time to time.

, ?

+4
1

, , , animationDidStop .

@property (weak, nonatomic) IBOutlet UIImageView *orangeView2;
@property (nonatomic) bool pulseActive;
@property (strong, nonatomic) CAKeyframeAnimation *pulseAnimation;

- , , , , - ( , ).

,

- (CAKeyframeAnimation *)pulseAnimation
{
    if ( !_pulseAnimation )
    {
        _pulseAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        _pulseAnimation.values = @[ @0.0f, @1.0f, @0.0f ];
        _pulseAnimation.duration = 0.5;

        _pulseAnimation.delegate = self;
        [_pulseAnimation setValue:@"PulseAnimation" forKey:@"AnimationIdentifier"];
    }

    return( _pulseAnimation );
}

  • ( )
  • removedOnCompletion ( )
  • delegate self, animationDidStop
  • setValue:forKey:

, , , animationDidStop. , forKey setValue, .

, animationDidStop. pulseActive ( ).

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
    NSString *animationIdentifier = [animation valueForKey:@"AnimationIdentifier"];

    if ( [animationIdentifier isEqualToString:@"PulseAnimation"] )
    {
        if ( self.pulseActive )
            [self.orangeView2.layer addAnimation:self.pulseAnimation forKey:@"pulsate"];
    }
}

. , ,

- (IBAction)pulseButtonPressed
{
    if ( !self.pulseActive )
    {
        self.pulseActive = YES;
        [self.orangeView2.layer addAnimation:[self pulseAnimation] forKey:@"pulsate"];
    }
    else
    {
        self.pulseActive = NO;
    }
}
+5

Source: https://habr.com/ru/post/1546364/


All Articles