Andrew
Andrew

Reputation: 16051

How can I re-size and position a UIBezierPath?

I have a UIBezierPath made from a SVG file using PocketSVG.

I want to include my UIBezierPath in a CGShapeLayer, and use this code:

self.centerIconShapeLayer = [CAShapeLayer layer];
self.centerIconShapeLayer.frame = CGRectMake(5, 8, self.frame.size.width - 10, self.frame.size.height - 16);
self.centerIconShapeLayer.fillColor = [[UIColor redColor] CGColor];
self.centerIconShapeLayer.contentsRect = CGRectMake(600, 0, 100, 100);
self.centerIconShapeLayer.contentsGravity = kCAGravityCenter;
[self.layer addSublayer:self.centerIconShapeLayer];
self.centerIconShapeLayer.path = myBezierPath.CGPath;

This doesn't seem to work though, and my shape doesn't change sizes or positions in relation to any of these settings.

Is there something I'm missing, in terms of functionality to move it into position?

Upvotes: 1

Views: 1614

Answers (1)

matt
matt

Reputation: 535138

The features like contentsGravity is about the layer's contents. A shape layer's contents is not its path; the path is not subject to that stuff. I suggest you use a transform instead. Or, don't use a shape layer: just draw your content straight into a layer's contents (let me know if you need more info on how to do that).

Also, you are using contentsRect wrong. It is not measured in points but in proportionality (each point coordinate is between 0 and 1 - well, okay, it's more complicated than that, but that's the basic idea).

Example that I tried, just to get you started (this is in a view controller, you will have to adapt everything I'm doing here!):

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.centerIconShapeLayer = [CALayer layer]; // ordinary layer
    self.centerIconShapeLayer.frame = 
        CGRectMake(5, 8, self.view.frame.size.width - 10, self.view.frame.size.height - 16);
    self.centerIconShapeLayer.delegate = self;
    [self.view.layer addSublayer:self.centerIconShapeLayer];
    // self.centerIconShapeLayer.contentsRect = CGRectMake(0, 0, 0.3, 0.3);
    [self.centerIconShapeLayer setNeedsDisplay];
}

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    UIBezierPath* myBezierPath = 
        [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,100,100) cornerRadius:20];
    CGContextAddPath(ctx,myBezierPath.CGPath);
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextFillPath(ctx);
}

Run that and you'll see the red rounded rect. Now uncomment the contentsRect line and you will see that it does indeed work to stretch the red rounded rect.

Of course you will have to substitute your own bezier path; this was just an example to show you how to draw a bezier path's path directly into a layer.

For more about how to draw directly into a layer, see this section of my book:

http://www.apeth.com/iOSBook/ch16.html#_drawing_in_a_layer

For more about drawing into a context generally, see my book's Drawing chapter, starting here:

http://www.apeth.com/iOSBook/ch15.html#_paths_and_drawing

Upvotes: 1

Related Questions