Ser Pounce
Ser Pounce

Reputation: 14527

Fitting View Frame around Scaled CALayer

In the following method, I'm simply enlarging a bunch of CAShapeLayers which are attached to my views layer property by using CATransform3DMakeScale. I'm able to enlarge the shape layers correctly, but am having a tough time getting the view frame to enlarge and fit around the graphic produced by the shape layers like it should. Everything is the same size, ie the view frame is the same size as all of the shape layers.

Anyone have any suggestions on how to fix this? I've tried altering the views transform property as well as the views layer transform property. Not sure what I'm missing here.

- (void) setSize:(CGSize)size
{
    CATransform3D transform = CATransform3DMakeScale(size.width / (self.graphic.initialSize.width), size.height / (self.graphic.initialSize.height), 1);
    self.layer.sublayerTransform = transform;
    self.frame = CGRectMake(0, 0, size.width, size.height);
    self.graphic.size = CGSizeMake(size.width, size.height);
}

Also for reference, here is how I am setting up the views layer:

- (id)initWithGraphic:(Graphic*)graphic
{
    self = [super initWithFrame:CGRectZero];
    if (self) 
    {
        self.backgroundColor = [UIColor clearColor];

        _graphic = [graphic retain];

        self.frame = CGRectMake(0,0, _graphic.initialSize.width, _graphic.initialSize.height);
        self.layer.shouldRasterize = YES;

        for (int i = 0; i < [_graphic.shapeLayers count]; i++)
        {
            [self.layer addSublayer:[_graphic.shapeLayers objectAtIndex:i]];
        }
    }
    return self;
}

and here is how I'm setting up one of the shape layers:

CAShapeLayer *outlineShapeLayer = [[CAShapeLayer alloc] init];
outlineShapeLayer.anchorPoint = CGPointMake(0, 0);
outlineShapeLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;
outlineShapeLayer.bounds = shapeLayerFrame;
outlineShapeLayer.mutablePath = mutablePath;

Upvotes: 2

Views: 325

Answers (1)

Ser Pounce
Ser Pounce

Reputation: 14527

I had to make it so my views layer also had it's anchorPoint and position set up to match my CAShapeLayers and then it worked:

self.layer.anchorPoint = CGPointMake(0, 0);
self.layer.position = CGPointMake(0, 0);

Upvotes: 1

Related Questions