iOS画图-UIBezierPath和CAShapeLayer的简单使用

UIBezierPath和CAShapeLayer组合画出任何你想要的图形,废话不多说,直接上代码:

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) cornerRadius:0];

    CGPoint startPoint = CGPointMake(KWidth/2, KHeight/2-105);
    CGPoint endPoint = CGPointMake(KWidth/2, KHeight/2+105);

    CGPoint controlPointLeft1 = CGPointMake(KWidth/2-170, KHeight/2-110);
    CGPoint controlPointLett2 = CGPointMake(KWidth/2-70, KHeight/2+110);

    CGPoint controlPointRight1 = CGPointMake(KWidth/2+170, KHeight/2-110);
    CGPoint controlPointRight2 = CGPointMake(KWidth/2+70, KHeight/2+110);

    UIBezierPath *FaceBezierPath = [UIBezierPath bezierPath];
    FaceBezierPath.lineWidth = 3;
    [[UIColor clearColor] setStroke];

    [FaceBezierPath moveToPoint:startPoint];
    [FaceBezierPath addCurveToPoint:endPoint controlPoint1:controlPointLeft1 controlPoint2:controlPointLett2];
    [FaceBezierPath moveToPoint:startPoint];
    [FaceBezierPath addCurveToPoint:endPoint controlPoint1:controlPointRight1 controlPoint2:controlPointRight2];
    [FaceBezierPath stroke];

    //虚线部分
    CAShapeLayer *vertureLineLayer = [CAShapeLayer layer];
    vertureLineLayer.path = FaceBezierPath.CGPath;
    vertureLineLayer.strokeColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:0.470].CGColor;
    vertureLineLayer.fillColor = [UIColor clearColor].CGColor;
    vertureLineLayer.lineCap = kCALineCapSquare;
    vertureLineLayer.lineWidth = 3;
    vertureLineLayer.lineDashPattern = @[@7,@10];
    [self.layer addSublayer:vertureLineLayer];

    [path appendPath:FaceBezierPath];

    //遮罩层
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor colorWithWhite:0.098 alpha:0.520].CGColor;
    [self.layer addSublayer:fillLayer];

温馨提示:上面的代码都是要写在UIView子类的- (void)drawRect:(CGRect)rect;方法里面的哦!

附上效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值