0
我使用下面的代碼繪製trinagle形狀,我想爲我使用bezierpath繪製的此形狀應用陰影。向uibiezierpath顯示shdow
CGContextRef context = UIGraphicsGetCurrentContext();
//creating a new nsshadow
NSShadow* shadow = [[NSShadow alloc] init];
[shadow setShadowColor: UIColor.blackColor];
[shadow setShadowOffset: CGSizeMake(3.1, 3.1)];
[shadow setShadowBlurRadius: 5];
//view to add a bezier path
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(67.0f,33.4f, 40.0f ,20.0f)];
// code for drawing a trinagular shape
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addQuadCurveToPoint:CGPointMake(7.0f, 3.0f) controlPoint:CGPointMake(4.0f, 0.0f)];
[trianglePath addLineToPoint:CGPointMake(17.0f,15.0f)];
[trianglePath addQuadCurveToPoint:CGPointMake(23.0f,15.0f) controlPoint:CGPointMake(20.0f, 20.0f)];
[trianglePath addLineToPoint:CGPointMake(33.0f, 3.0f)];
[trianglePath addQuadCurveToPoint:CGPointMake(40.0, 0.0f) controlPoint:CGPointMake(36.5f, 0.0f)];
[trianglePath addLineToPoint:CGPointMake(0.0f, 0.0f)];
CGContextAddPath(context, trianglePath.CGPath);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow.shadowOffset, shadow.shadowBlurRadius, [shadow.shadowColor CGColor]);
[UIColor.redColor setFill];
[trianglePath fill];
CGContextRestoreGState(context);
[UIColor.blackColor setStroke];
trianglePath.lineWidth = 1;
[trianglePath stroke];
CGContextFillPath(context);
//CAShapeLayer to set the bezier path
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
triangleMaskLayer.lineWidth = 0.1f;
triangleMaskLayer.fillColor = [[UIColor whiteColor] CGColor];
[triangleView.layer addSublayer:triangleMaskLayer];
我想爲這個uibezier路徑設置一個陰影嗎?我嘗試過使用某些方法,但無法弄清楚什麼是錯誤的?