I use the following code to draw a triangle shape,I want to apply shadow for this shape i have drawn using bezierpath.
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 triangular 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];
I want to set a shadow for this uibezier path? I have tried using some ways but unable to figure out what,s going wrong?
try by setting shadow properties to CAShapeLayer
like below:
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
triangleMaskLayer.shadowColor=[UIColor blackColor].CGColor;
triangleMaskLayer.shadowOffset=CGSizeMake(3.1, 3.1);
triangleMaskLayer.shadowRadius=5.0;
[triangleView.layer addSublayer:triangleMaskLayer];