views:

110

answers:

1

Should I be able to override drawInContext() and draw outside the bounds of my CALayer? Even though my layer has maskToBounds set to NO (the default) my drawInContext() is called with a clip set to the bounds of my layer and I am unable to draw outside of it.

My test layer does something like this:

-(void)drawInContext:(CGContextRef)context 
{   
    [super drawInContext:context];      
    NSLog(@"mask to bounds=%d", self.masksToBounds); // NO  
    CGRect clip = CGContextGetClipBoundingBox(context);
    NSLog(@"clip=%f,%f,%f,%f", clip.origin.x, clip.origin.y, clip.size.width, clip.size.height); // reports the bounds

    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);  
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 500, 0.0); // wider than my layer...
    CGContextStrokePath(context);
}

and here is how I set it up:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    CALayer *layer = [[MyLayer alloc] init];
    layer.needsDisplayOnBoundsChange=YES;
    layer.frame = CGRectMake(50, 50, 200, 200);
    [self.view.layer addSublayer:layer];
}

Is this just a limitation of core animation layers? (Do I need to draw in the layer above this layer?)

thanks.

+1  A: 

There is no way for a layer's contents to overflow its bounds; however, you can add a sublayer that overflows by specifying a frame that stretches outside the bounds and setting the masksToBounds property to NO (the default).

rpetrich