我正在尝试使用UIBezierPath绘制自定义形状: UIBezierPath *aPath = [UIBezierPath bezierPath]; [aPath moveToPoint:CGPointMake(100.0, 0.0)]; // Draw the lines. [aPath addLineToPoint:CGPointMake(200.0, 40.0)]; [aPath addLineToPoin
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 }