gpt4 book ai didi

ios - 用平行线填充 UIBezierPath

转载 作者:可可西里 更新时间:2023-11-01 06:24:31 24 4
gpt4 key购买 nike

我正在尝试使用 UIBezierPath 绘制自定义形状:

UIBezierPath *aPath = [UIBezierPath bezierPath];

[aPath moveToPoint:CGPointMake(100.0, 0.0)];

// Draw the lines.
[aPath addLineToPoint:CGPointMake(200.0, 40.0)];
[aPath addLineToPoint:CGPointMake(160, 140)];
[aPath addLineToPoint:CGPointMake(40.0, 140)];
[aPath addLineToPoint:CGPointMake(0.0, 40.0)];
[aPath closePath];

我想用平行线填充它以使其剥离。我也想改变这条线的颜色。假设我想让它们垂直。我必须以某种方式定期计算这条路径上的点,我该怎么做?

我找到了这个 UIColor colorWithPatternImage,但我无法更改形状内线条的颜色和“密度”。

最佳答案

就像 Nikolai Ruhe 所说,最好的选择是使用您的形状作为剪切路径,然后在形状的边界框内绘制一些图案。这是代码的示例

- (void)drawRect:(CGRect)rect
{
// create a UIBezierPath for the outline shape
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(100.0, 0.0)];
[aPath addLineToPoint:CGPointMake(200.0, 40.0)];
[aPath addLineToPoint:CGPointMake(160, 140)];
[aPath addLineToPoint:CGPointMake(40.0, 140)];
[aPath addLineToPoint:CGPointMake(0.0, 40.0)];
[aPath closePath];
[aPath setLineWidth:10];

// get the bounding rectangle for the outline shape
CGRect bounds = aPath.bounds;

// create a UIBezierPath for the fill pattern
UIBezierPath *stripes = [UIBezierPath bezierPath];
for ( int x = 0; x < bounds.size.width; x += 20 )
{
[stripes moveToPoint:CGPointMake( bounds.origin.x + x, bounds.origin.y )];
[stripes addLineToPoint:CGPointMake( bounds.origin.x + x, bounds.origin.y + bounds.size.height )];
}
[stripes setLineWidth:10];

CGContextRef context = UIGraphicsGetCurrentContext();

// draw the fill pattern first, using the outline to clip
CGContextSaveGState( context ); // save the graphics state
[aPath addClip]; // use the outline as the clipping path
[[UIColor blueColor] set]; // blue color for vertical stripes
[stripes stroke]; // draw the stripes
CGContextRestoreGState( context ); // restore the graphics state, removes the clipping path

// draw the outline of the shape
[[UIColor greenColor] set]; // green color for the outline
[aPath stroke]; // draw the outline
}

使用 Swift

override func draw(_ rect: CGRect){

// create a UIBezierPath for the outline shape
let aPath = UIBezierPath()
aPath.move(to: CGPoint(x: 100.0, y: 0.0))
aPath.addLine(to: CGPoint(x: 200.0, y: 40.0))
aPath.addLine(to: CGPoint(x: 160, y: 140))
aPath.addLine(to: CGPoint(x: 40.0, y: 140))
aPath.addLine(to: CGPoint(x: 0.0, y: 40.0))
aPath.close()
aPath.lineWidth = 10

// get the bounding rectangle for the outline shape
let bounds = aPath.bounds

// create a UIBezierPath for the fill pattern
let stripes = UIBezierPath()
for x in stride(from: 0, to: bounds.size.width, by: 20){
stripes.move(to: CGPoint(x: bounds.origin.x + x, y: bounds.origin.y ))
stripes.addLine(to: CGPoint(x: bounds.origin.x + x, y: bounds.origin.y + bounds.size.height ))
}
stripes.lineWidth = 10

let context = UIGraphicsGetCurrentContext()

// draw the fill pattern first, using the outline to clip
context!.saveGState() // save the graphics state
aPath.addClip() // use the outline as the clipping path
UIColor.blue.set() // blue color for vertical stripes
stripes.stroke() // draw the stripes
context!.restoreGState() // restore the graphics state, removes the clipping path

// draw the outline of the shape
UIColor.green.set() // green color for the outline
aPath.stroke() // draw the outline
}

生产

enter image description here

关于ios - 用平行线填充 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25542801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com