Krumelur
Krumelur

Reputation: 33058

Subclass of UIView with CAShapeLayer as mask does not animate correctly

I have added round corners to my subclassed UIView by adding a layer mask. The following is Montouch code, but should be easy enough to get the meaning also in ObjC:

// Add a layer that holds the rounded corners.
UIBezierPath oMaskPath = UIBezierPath.FromRoundedRect (this.Bounds, this.eRoundedCorners, new SizeF (this.fCornerRadius, this.fCornerRadius));

private void UpdateMask()
{           
if(this.Layer.Mask == null)
{
    CAShapeLayer oMaskLayer = new CAShapeLayer ();
    oMaskLayer.Frame = this.Bounds;

    // Set the newly created shape layer as the mask for the image view's layer
    this.Layer.Mask = oMaskLayer;
}
((CAShapeLayer)this.Layer.Mask).Path = oMaskPath.CGPath;
}

I have overloaded the Frame property. Setting the frame adjusts the mask to keep the rounded corners in shape.

However if I animate the change in frame size, the mask is immediately set to the destination value of the animation instead.

Is there a way to make my view detect that is is part of an animation and make the mask animate correctly?

Upvotes: 0

Views: 838

Answers (1)

miguel.de.icaza
miguel.de.icaza

Reputation: 32694

This is how CoreAnimation works for explicit animations.

With explicit animations, you never actually alter the values of your original objects, you merely animate objects in the presentation layer. So you need to make sure that your object has been setup before you start the animation with the value that you want to have in the end.

A complete explanation of how to solve this class of problems is explained in "Core Animation Essentials" on the WWDC 2011 online videos. Look for session 421

Upvotes: 1

Related Questions