Note that there are some explanatory texts on larger screens.

plurals
  1. POAnimating CALayer's shadowPath property
    primarykey
    data
    text
    <p>I am aware that a CALayer's shadowPath is only animatable using explicit animations, however I still cannot get this to work. I suspect that I am not passing the <code>toValue</code> properly - as I understand this has to be an <code>id</code>, yet the property takes a CGPathRef. Storing this in a <code>UIBezierPath</code> does not seem to work. I am using the following code to test:</p> <pre><code>CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; theAnimation.duration = 3.0; theAnimation.toValue = [UIBezierPath bezierPathWithRect:CGRectMake(-10.0, -10.0, 50.0, 50.0)]; [self.view.layer addAnimation:theAnimation forKey:@"animateShadowPath"]; </code></pre> <p>(I am using minus values so as to ensure the shadow extends beyond a view that lies on top of it... the layer's <code>masksToBounds</code> property is set to <code>NO</code>).</p> <p>How is animation of the shadowPath achieved?</p> <p><strong>UPDATE</strong></p> <p>Problem <em>nearly</em> solved. Unfortunately, the main problem was a somewhat careless error...</p> <p>The mistake I made was to add the animation to the root layer of the view controller, rather than the layer I had dedicated to the shadow. Also, @pe8ter was correct in that the <code>toValue</code> needs to be a <code>CGPathRef</code> cast to <code>id</code> (obviously when I had tried this before I still had no animation due to the wrong layer mistake). The animation works with the following code:</p> <pre><code>CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; theAnimation.duration = 3.0; theAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:myRect].CGPath; [controller.shadowLayer addAnimation:theAnimation forKey:@"shadowPath"]; </code></pre> <p>I appreciate this was difficult to spot from the sample code I provided. Hopefully it can still be of use to people in a similar situation though.</p> <p>However, when I try and add the line</p> <pre><code>controller.shadowLayer.shadowPath = [UIBezierPath bezierPathWithRect:myRect].CGPath; </code></pre> <p>the animation stops working, and the shadow just jumps to the final position instantly. Docs say to add the animation with the same key as the property being changed so as to override the implicit animation created when setting the value of the property, however shadowPath can't generate implicit animations... so how do I get the new property to stay <em>after</em> the animation?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload