如何重新调整UIBezierPath的大小和位置?

时间:2021-12-06 21:25:59

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

我有一个使用PocketSVG从SVG文件制作的UIBezierPath。

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

我想在CGShapeLayer中包含我的UIBezierPath,并使用以下代码:

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?

是否有一些我缺少的东西,在功能方面将它移动到位?

1 个解决方案

#1


1  

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).

像contentsGravity这样的功能是关于图层的内容。形状层的内容不是它的路径;路径不受那些东西的影响。我建议你改用变换。或者,不要使用形状图层:只需将内容直接绘制到图层的内容中(如果您需要有关如何操作的更多信息,请告诉我)。

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).

此外,您正在使用contentsRect错误。它不是用点来衡量的,而是按比例测量的(每个点坐标都在0到1之间 - 好吧,它比这更复杂,但那是基本的想法)。

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.

运行它,你会看到红色圆角矩形。现在取消注释contentsRect行,您将看到它确实可以拉伸红色圆角矩形。

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.

当然,你必须用你自己的bezier路径代替;这只是一个示例,向您展示如何将bezier路径的路径直接绘制到图层中。

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

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

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

#1


1  

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).

像contentsGravity这样的功能是关于图层的内容。形状层的内容不是它的路径;路径不受那些东西的影响。我建议你改用变换。或者,不要使用形状图层:只需将内容直接绘制到图层的内容中(如果您需要有关如何操作的更多信息,请告诉我)。

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).

此外,您正在使用contentsRect错误。它不是用点来衡量的,而是按比例测量的(每个点坐标都在0到1之间 - 好吧,它比这更复杂,但那是基本的想法)。

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.

运行它,你会看到红色圆角矩形。现在取消注释contentsRect行,您将看到它确实可以拉伸红色圆角矩形。

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.

当然,你必须用你自己的bezier路径代替;这只是一个示例,向您展示如何将bezier路径的路径直接绘制到图层中。

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

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

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