使用CoreGraphics在IOS中使用Crosshatch?

时间:2016-06-15 02:25:10

标签: ios uiview core-graphics gradient drawrect

如何使用核心图形在IOS中填充形状的横截面(在45度处应用一组平行线)?示例代码?

(我特别感兴趣的是在MKMapKit中使用MKPolygon,但目前只是想在UIView中使用drawRect查看它是否可行。所以请填写带有交叉线的UIView的背景#39;荷兰国际集团)

4 个答案:

答案 0 :(得分:4)

for swift 3.,使用来自@ user3230875的方法

final class CrossHatchView: UIView {

    // MARK: - LifeCycle

    override func draw(_ rect: CGRect) {
        let path:UIBezierPath = UIBezierPath(roundedRect: bounds, cornerRadius: 5)
        path.addClip()

        let pathBounds = path.bounds
        path.removeAllPoints()
        let p1 = CGPoint(x:pathBounds.maxX, y:0)
        let p2 = CGPoint(x:0, y:pathBounds.maxX)
        path.move(to: p1)
        path.addLine(to: p2)
        path.lineWidth = bounds.width * 2

        let dashes:[CGFloat] = [0.5, 7.0]
        path.setLineDash(dashes, count: 2, phase: 0.0)
        UIColor.lightGray.withAlphaComponent(0.5).set()
        path.stroke()
    }
}

结果:

enter image description here

答案 1 :(得分:3)

以您想要的任何方式创建包含交叉线图案的UIImage(例如,使用Core Graphics绘制或从PNG文件加载)。

然后使用+[UIColor colorWithPatternImage:](Swift UIColor(patternImage:))创建绘制交叉影线图像的“颜色”。

最后,将图案颜色设置为填充颜色,并填充形状(可能通过填充勾勒出形状的路径,或使用UIRectFill)。

如果您需要对模式进行更多控制(更改其平铺或对齐方式),您可以下拉到核心图形级别并使用CGPatternCreateCGColorCreateWithPattern

答案 2 :(得分:2)

嘿试试我在300x300 UIView上试过的示例代码

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.5);
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    int backward=0;

    for (int i=0;i<15; i++)
    {
        CGContextMoveToPoint(context, backward, 0);
        CGContextAddLineToPoint(context, 300, 300-backward);
        backward=backward+20;
    }
    int backwardNegitive=0;
    for (int i=0;i<15; i++)
    {
        CGContextMoveToPoint(context, 0,backwardNegitive);
        CGContextAddLineToPoint(context, 300-backwardNegitive,300);
        backwardNegitive=backwardNegitive+20;
    }
    int forward=0;
    for (int i=0;i<15; i++)
    {
        CGContextMoveToPoint(context, 300-forward, 0);
        CGContextAddLineToPoint(context, 0, 300-forward);
        forward=forward+20;
    }
    int forwardNegative=0;
    for (int i=0;i<15; i++)
    {
        CGContextMoveToPoint(context, 0,300+forwardNegative);
        CGContextAddLineToPoint(context,300+forwardNegative,0);
        forwardNegative=forwardNegative+20;
    }
    CGContextStrokePath(context);

}

希望这对你有所帮助。

答案 3 :(得分:2)

以下是我在Apple开发者论坛上讨论的内容:

#import "CrossHatchView.h"

@implementation CrossHatchView

static  CGFloat  sides = 5.0;

- (void)drawRect:(CGRect)rect
{
    CGRect  bounds = self.bounds;

    UIBezierPath  *path = [UIBezierPath bezierPath];

    CGFloat  xCentre = CGRectGetMidX(bounds);
    CGFloat  yCentre = CGRectGetMidY(bounds);
    CGFloat  radius = 0.0;

    if (CGRectGetWidth(bounds) > CGRectGetHeight(bounds)) {
        radius = CGRectGetHeight(bounds) / 2.0;
    } else {
        radius = CGRectGetWidth(bounds) / 2.0;
    }
    CGFloat  angleIncrement = 2.0 * M_PI / sides;

    CGFloat  initialAngle = ( M_PI + (2.0 * M_PI / sides) ) / 2.0;

    for (NSUInteger  i = 0;  i < sides;  i++) {
        CGFloat  angle = initialAngle + i * angleIncrement;
        CGFloat  x = xCentre + radius * cos(angle);
        CGFloat  y = yCentre + radius * sin(angle);
        CGPoint  point = CGPointMake(x, y);
        if (i == 0) {
            [path moveToPoint:point];
        } else {
            [path addLineToPoint:point];
        }
    }
    [path closePath];
    [[UIColor cyanColor] set];
    [path addClip];

    CGRect  pathBounds = [path bounds];

    [path removeAllPoints];
    CGPoint  p1 = pathBounds.origin;
    CGPoint  p2 = CGPointMake(CGRectGetMaxX(pathBounds), CGRectGetMaxY(pathBounds));
    [path moveToPoint:p1];
    [path addLineToPoint:p2];
    path.lineWidth = 400.0;
    CGFloat  dashes[] = { 2.0, 2.0 };
    [path setLineDash:dashes count:2 phase:0.0];
    [[UIColor blackColor] set];
    [path stroke];
}

@end