Brosef
Brosef

Reputation: 3095

Rendering a CAShapeLayer within custom UIView

I have a custom UIView within a custom tableViewCell. I'm trying to render two CAShapeLayers but nothing is showing up.

-(void) setUpLayers
{
if (!self.blueWave) {

    self.blueWave = [self createShapeLayer];

    self.blueWave.strokeColor = [UIColor blueColor].CGColor;

    self.redWave = [self createShapeLayer];
    self.redWave.strokeColor = [UIColor redColor].CGColor;

    [self.layer addSublayer:self.blueWave];
    [self.layer insertSublayer:self.redWave above:self.blueWave];

    [self render]; 
    } 
} 


- (CAShapeLayer *) createShapeLayer {
    CAShapeLayer* layer = [[CAShapeLayer alloc] init];
    layer.lineWidth = 2;
    layer.fillColor = nil;


    return layer;
}

This code works fine when its placed within layoutSubviews or layoutSublayersOfLayer in my custom UIView class, but not within my custom method above. Any idea why?

Upvotes: 0

Views: 244

Answers (1)

Devin
Devin

Reputation: 136

I can't tell what

[self createShapeLayer] 

is doing, but be sure you are setting the CAShapeLayer's frame to something like self.bounds. If you are using self.frame, there is the possibility that the layers are just being rendered off-screen.

Alternatively, I've used the following code which might be similar to what you're looking to do

CAShapeLayer *rectangle = [CAShapeLayer layer];
UIBezierPath *rectanglePath=[UIBezierPath bezierPathWithRect:self.bounds];
rectangle.path=rectanglePath.CGPath;
rectangle.fillColor = nil;
rectangle.strokeColor = [UIColor redColor].CGColor;
rectangle.lineWidth = 2.0;
rectangle.opacity = 1.0;
[layer addSublayer:rectangle];

Be sure that at the point you are creating this shapeLayer that your bounds actually has size to it as well.

Upvotes: 1

Related Questions