我有一个奇怪的问题.虽然我确实阅读了很多关于如何做到这一点的教程,但最终结果只显示了贝塞尔线,而不是任何阴影.我的代码非常简单: let borderLine = UIBezierPath() borderLine.moveToPoint(
let borderLine = UIBezierPath() borderLine.moveToPoint(CGPoint(x:0, y: y! - 1)) borderLine.addLineToPoint(CGPoint(x: x!, y: y! - 1)) borderLine.lineWidth = 2 UIColor.blackColor().setStroke() borderLine.stroke() let shadowLayer = CAShapeLayer() shadowLayer.shadowOpacity = 1 shadowLayer.shadowOffset = CGSize(width: 0,height: 1) shadowLayer.shadowColor = UIColor.redColor().CGColor shadowLayer.shadowRadius = 1 shadowLayer.masksToBounds = false shadowLayer.shadowPath = borderLine.CGPath self.layer.addSublayer(shadowLayer)
我做错了什么,因为我似乎没有看到任何错误但当然我错了,因为没有阴影出现.函数是drawRect,基本的UIVIew没有任何额外的东西,x和y是框架的宽度和高度.提前谢谢了!
我直接从我的PaintCode应用程序中获取此示例.希望这可以帮助.//// General Declarations let context = UIGraphicsGetCurrentContext() //// Shadow Declarations let shadow = UIColor.blackColor() let shadowOffset = CGSizeMake(3.1, 3.1) let shadowBlurRadius: CGFloat = 5 //// Bezier 2 Drawing var bezier2Path = UIBezierPath() bezier2Path.moveToPoint(CGPointMake(30.5, 90.5)) bezier2Path.addLineToPoint(CGPointMake(115.5, 90.5)) CGContextSaveGState(context) CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, (shadow as UIColor).CGColor) UIColor.blackColor().setStroke() bezier2Path.lineWidth = 1 bezier2Path.stroke() CGContextRestoreGState(context)