Search code examples
cocoa-touchcore-animationcore-graphics

Core Animation with a Core Graphics drawing


I am inside a UIView subclass (CustomView in the code below).

I am drawing a border around an image in a UIImageView subclass (containerView). The Core Graphics code for this is located inside drawInRect: in the subclass.

The Core Animation transition is a flip, however when it is drawn, the Core Graphics code is not used, i.e. drawInRect: is not called to provide the CALayer with my rendered image.

Instead I get the animation from one image to the other without any Core Graphics drawing occurring.

How do I allow the CALayer to animate using my Core Graphics drawing rather than the raw image?

[CustomView beginAnimations:@"transitionNextImage" context:nil];
[CustomView setAnimationDuration:2.0];
[containerView setImage:image];
[CustomView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
[containerView setImage:differentImage];
[CustomView commitAnimations];

Solution

  • You're implementing drawRect:, not drawInRect:, right? Also, have you verified that outside of this transition, it actually works?

    Can you paste your drawRect:implementation? I'm just afraid that UIImageView sets the contents of its layer directly instead of relying on drawRect: to do it, which would mean your drawRect: doesn't work.

    In fact, given how simple this actually is, I would recommend you just subclass UIView instead of UIImageView. The math to preserve aspect ratio when scaling the image for drawing is pretty simple.

    Another option is, of course, to subclass UIImageView and then just override setImage: to create a new image based on the first but drawing a border around that, and then passing that to super. That way you don't have to do anything with drawRect: at all.