Search code examples
iosc4

Artifacts when animating shapes in C4 - c4framework


When animating a shape in C4 the undefined preset characteristics of the shape, such as line width and fillcolour, also animate. Is there a work around or solution to this problem?


Solution

  • C4Shapes are setup to use the default colors C4Red (for the strokeColor) and C4Blue (for the fillColor). Also, the default animationDuration is set to 0.25 seconds.

    Technically, what's happening, is the C4Shape is constructing itself with C4Red / C4Blue colors and then initiates an animation from those to any new colors as soon as it hits the canvas.

    To get around these, and to generate your own settings you can subclass C4Shape and add the coloring / timing / other property changes to your class's own init method.

    In the implementation (.m file) of a MyShape class I have:

    @implementation MyShape
    -(id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if(self != nil) {
            self.animationDuration = 0.0f;
            self.fillColor = [UIColor purpleColor];
            self.strokeColor = [UIColor greenColor];
        }
        return self;
    }
    @end
    

    ... and my C4WorkSpace.m looks like this:

    #import "C4WorkSpace.h"
    #import "MyShape.h"
    
    @implementation C4WorkSpace
    
    -(void)setup {
        MyShape *ms = [MyShape new];
        [ms ellipse:CGRectMake(100, 100, 100, 100)];
        [self.canvas addShape:ms];
    }
    
    @end
    

    I know this is a bit crude at the moment, but we haven't worked through setting the default colors before the object hits the canvas.