向 uibezierpath 显示阴影
showing shdow to uibiezierpath
我使用以下代码绘制一个三角形,我想为我使用 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];
我想给这个uibezier路径设置阴影?我尝试过使用一些方法,但无法弄清楚哪里出了问题?
尝试将阴影属性设置为 CAShapeLayer
,如下所示:
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
triangleMaskLayer.shadowColor=[UIColor blackColor].CGColor;
triangleMaskLayer.shadowOffset=CGSizeMake(3.1, 3.1);
triangleMaskLayer.shadowRadius=5.0;
[triangleView.layer addSublayer:triangleMaskLayer];
SWIFT 3
你可以先创建一个阴影层,然后你可以添加你的形状层作为这个阴影层的子层。其他的就直接了。
let triangleView = UIView(frame: CGRect(x: 67, y: 33, width: 40, height: 20))
let trianglePath = UIBezierPath()
trianglePath.move(to: CGPoint(x:0, y:0))
trianglePath.addQuadCurve(to: CGPoint(x:7, y:3), controlPoint: CGPoint(x:4, y:0))
trianglePath.addLine(to: CGPoint(x:17, y:15))
trianglePath.addQuadCurve(to: CGPoint(x:23, y:15), controlPoint: CGPoint(x:20, y:20))
trianglePath.addLine(to: CGPoint(x:33, y:3))
trianglePath.addQuadCurve(to: CGPoint(x:40, y:0), controlPoint: CGPoint(x:36, y:0))
trianglePath.addLine(to: CGPoint(x:0, y:0))
trianglePath.close()
//CAShapeLayer to set the bezier path
let triangleMaskLayer = CAShapeLayer()
triangleMaskLayer.path = trianglePath.cgPath
triangleMaskLayer.lineWidth = 1
triangleMaskLayer.strokeColor = UIColor.black.cgColor
triangleMaskLayer.fillColor = UIColor.red.cgColor
let shadowlayer = CALayer()
shadowlayer.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
shadowlayer.shadowColor = UIColor.black.cgColor
shadowlayer.shadowRadius = 5.0
shadowlayer.shadowOffset = CGSize(width: 3.1, height: 3.1)
shadowlayer.shadowOpacity = 1.0
shadowlayer.addSublayer(triangleMaskLayer)
triangleView.layer.addSublayer(shadowlayer)
self.view .addSubview(triangleView)
我使用以下代码绘制一个三角形,我想为我使用 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];
我想给这个uibezier路径设置阴影?我尝试过使用一些方法,但无法弄清楚哪里出了问题?
尝试将阴影属性设置为 CAShapeLayer
,如下所示:
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
triangleMaskLayer.shadowColor=[UIColor blackColor].CGColor;
triangleMaskLayer.shadowOffset=CGSizeMake(3.1, 3.1);
triangleMaskLayer.shadowRadius=5.0;
[triangleView.layer addSublayer:triangleMaskLayer];
SWIFT 3 你可以先创建一个阴影层,然后你可以添加你的形状层作为这个阴影层的子层。其他的就直接了。
let triangleView = UIView(frame: CGRect(x: 67, y: 33, width: 40, height: 20))
let trianglePath = UIBezierPath()
trianglePath.move(to: CGPoint(x:0, y:0))
trianglePath.addQuadCurve(to: CGPoint(x:7, y:3), controlPoint: CGPoint(x:4, y:0))
trianglePath.addLine(to: CGPoint(x:17, y:15))
trianglePath.addQuadCurve(to: CGPoint(x:23, y:15), controlPoint: CGPoint(x:20, y:20))
trianglePath.addLine(to: CGPoint(x:33, y:3))
trianglePath.addQuadCurve(to: CGPoint(x:40, y:0), controlPoint: CGPoint(x:36, y:0))
trianglePath.addLine(to: CGPoint(x:0, y:0))
trianglePath.close()
//CAShapeLayer to set the bezier path
let triangleMaskLayer = CAShapeLayer()
triangleMaskLayer.path = trianglePath.cgPath
triangleMaskLayer.lineWidth = 1
triangleMaskLayer.strokeColor = UIColor.black.cgColor
triangleMaskLayer.fillColor = UIColor.red.cgColor
let shadowlayer = CALayer()
shadowlayer.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
shadowlayer.shadowColor = UIColor.black.cgColor
shadowlayer.shadowRadius = 5.0
shadowlayer.shadowOffset = CGSize(width: 3.1, height: 3.1)
shadowlayer.shadowOpacity = 1.0
shadowlayer.addSublayer(triangleMaskLayer)
triangleView.layer.addSublayer(shadowlayer)
self.view .addSubview(triangleView)