CABasicAnimation not animating my property

后端 未结 3 996
春和景丽
春和景丽 2021-02-09 02:39

I\'ve been trying to understand what is wrong with my animation and I still haven\'t figure it out. I think it should be really straight forward, but there is probably something

3条回答
  •  天命终不由人
    2021-02-09 03:08

    So, the solution was to animate the @"bounds" and the @"position" of the layer because frame is not animatable on iPhone. It took me some time to understand that the position was the center of the layer and the resize of the bounds was extending from the center, but that was the easy part.

    So, what I did in resume was:

    1. In the setFrame, create 2 animations with the bounds and position property.
    2. Use "animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards;" to ensure that the values are not reseted at the end of the animation
    3. Register the delegate to self in order to implements "animationDidStop:finished:". It seems that you still need to set the values: "layerImage.bounds = [animation.toValue CGRectValue]; layerImage.position = [animation.toValue CGPointValue];".

    I wasn't able to use the UIView animation system directly because it wasn't doing what I wanted on the layers.

    Thanks tadej5553 for pointing me out the layer problem I had with the "addAnimation". So here is the code for those who would like to see what it looks like.

    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
        CABasicAnimation *animation = (CABasicAnimation*)anim;
    
        if ([animation.keyPath isEqualToString:@"bounds"]) {
            layerImage.bounds = [animation.toValue CGRectValue];
        } else if ([animation.keyPath isEqualToString:@"position"]) {
            layerImage.position = [animation.toValue CGPointValue];
        }
    }
    
    - (void)setFrame:(CGRect)value {
        CGRect bounds = CGRectMake(0, 0, value.size.width, value.size.height);
    
        if ([UIView isAnimationStarted]) {
            // animate the bounds
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
            animation.duration = [UIView animationDuration];
            animation.fromValue = [NSValue valueWithCGRect:layerImage.bounds];
            animation.toValue = [NSValue valueWithCGRect:bounds];
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeForwards;
            animation.timingFunction = [UIView animationFunction];
            animation.delegate = self;
            [layerImage addAnimation:animation forKey:@"BoundsAnimation"];
    
            // animate the position so it stays at 0, 0 of the frame.
            animation = [CABasicAnimation animationWithKeyPath:@"position"];
            animation.duration = [UIView animationDuration];
            animation.fromValue = [NSValue valueWithCGPoint:layerImage.position];
            animation.toValue = [NSValue valueWithCGPoint:CGPointMake(bounds.size.width / 2, bounds.size.height / 2)];
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeForwards;
            animation.timingFunction = [UIView animationFunction];
            animation.delegate = self;
            [layerImage addAnimation:animation forKey:@"PositionAnimation"];
        } else {
            layerImage.frame = bounds;
        }
    
        [super setFrame:value];
    }
    

提交回复
热议问题