简体   繁体   中英

How to stroke the boundary of a uibezierpath on the inside of the path?

通常,当我们在CoreGraphics中绘制一个bezier路径,并将此'CGContextSetLineWidth'设置为路径的所需厚度时,它会在路径的外边界上划线(随着线宽增加,边界似乎在外部增长)路径),我希望我的线条厚度在bezier路径的内部生长,有没有办法做到这一点?

I am not aware of a way to stroke the inside of a path. However, you can accomplish something similar, which might work for you. If you scale down the path and move it to the center with the correct amount, it will fit nicely within the bounds you want. The visual difference between drawing inside and scaling is none for simple shapes like rectangles, rounded rects and ellipses, but will differ for more complex shapes. Consider what will happen if you stroke the letter B with "inside strokes", versus scaling it.

Here is what it looks like before and after transforming using a very wide line. As you can see, stroking a line will center it on the path, making half of it appear on each side. So to transform it, we will need to move the path by half a line width down and right, then scale it down by the line width.

Using bezierPathWithRoundedRect
line width 20, box size 200 x 300, corner radius 50

路径

The transform then becomes like this.

viewSize is your bounding box for the path
lineWidth is the width of the line
bezierPath is your UIBezierPath

CGAffineTransform transform = CGAffineTransformMakeTranslation(lineWidth / 2.0,
                                                               lineWidth / 2.0);
transform = CGAffineTransformScale(transform, 
                                   (viewSize.width - lineWidth) / viewSize.width, 
                                   (viewSize.height - lineWidth) / viewSize.height);

CGPathRef reducedPath = CGPathCreateCopyByTransformingPath(bezierPath.CGPath, &transform);


UPDATE

If you want to keep the aspect ratio, the scaling can be modified to be equal on both axis, using the smallest factor on both.

 CGFloat scale = viewSize.width < viewSize.height ? (viewSize.width - lineWidth) / viewSize.width : (viewSize.height - lineWidth) / viewSize.height; CGAffineTransform transform = CGAffineTransformMakeTranslation(lineWidth / 2.0, lineWidth / 2.0); transform = CGAffineTransformScale(transform, scale, scale); CGPathRef reducedPath = CGPathCreateCopyByTransformingPath(bezierPath.CGPath, &transform); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM