为什么CAShapeLayer的笔划延伸到框架之外? [英] Why does a CAShapeLayer's stroke extend out of the frame?

查看:129
本文介绍了为什么CAShapeLayer的笔划延伸到框架之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下为示例代码:

//Called by VC:

HICircleView *circleView = [[HICircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

// init of circle view

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.fillColor = [UIColor whiteColor].CGColor;
        borderLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.frame].CGPath;
        borderLayer.strokeColor = [[UIColor redColor] CGColor];
        borderLayer.lineWidth = 5;
        [self.layer addSublayer:borderLayer];
    }
    return self;
}

好的,谢谢你的回答。转移i:

OK, thanks for the answer. to shift i:

CGRect rect = CGRectMake(3, 3, self.frame.size.width, self.frame.size.height);
borderLayer.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;

并将线宽设为6。

推荐答案

设置 lineWidth 绘制一条线,其中实际路径恰好位于绘制线的中间。

Setting the lineWidth draws a line, where the actual path is exactly in the middle of the drawn line.

如果您希望绘制的线与某些东西对齐,则必须将路径移动 lineWidth 的一半。

If you want the drawn line to line up with something, you will have to shift the path by half the lineWidth.

您可以在 UIBezierPath上使用 - (void)applyTransform:(CGAffineTransform)transform 来移动路径并应用翻译变换。

You can shift the path by using - (void)applyTransform:(CGAffineTransform)transform on UIBezierPath and apply a translate transform.

如果您想要在某个区域中包含绘制的路径,则移动路径无济于事。在这种情况下只需创建一个较小的路径。如果要绘制线宽为5的100ptx100pt矩形,则必须在95pt * 95pt矩形中绘制一条路径(两边各2.5pt)。

If you want a drawn path to be contained in a certain area, shifting the path doesn't help. In that case just create a smaller path. If you want to draw a 100ptx100pt rect with a line width of 5, you have to draw a path in a 95pt*95pt rect (2.5pt space on either side).

这篇关于为什么CAShapeLayer的笔划延伸到框架之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆