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;方法里面的哦!
附上效果图: