At the moment i'm overriding drawRect
in a view in order to be able to clip a colored rectangle with a mask (using CGContextClipToMask
inside drawRect
).
Sometimes I change the color of this clipped rectangle. In this case drawRect
gets called again, redrawing and clipping the rectangle with the new color.
Now I don't want to change the color at once, but animate this. The problem is, that animations are not performed when overriding drawRect (drawRect only gets called one time and immediately).
Is there a way to perform this animation, maybe by subclassing the view, so that I still override drawRect
in the superclass, but the animation is somehow performed through the subclass so that drawRect from the superclass gets performed multiple times during the animation?
Or is also possible not to override drawRect
at all, and still be able to clip this rectangle with the mask somehow?
drawRect
looks something like this:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGImageRef maskImage = [[UIImage imageNamed:maskName] CGImage];
CGContextClipToMask(ctx, rect, maskImage);
CGContextSetFillColorWithColor(ctx, self.currentColor);
CGContextFillRect( ctx, rect );
I think it is better to use a CALayer to mask your view (don't forget to import QuartzCore):
maskLayer.contents = (id)[UIImage imageNamed:maskName].CGImage; // maskLayer is a CALayer
view.layer.mask = maskLayer;
Now assuming you have a method -(void)animate
in your view controller, you have execute it multiple times using a timer, or using recursive calls to performSelector:withObject:afterDelay
.