Swift 将阴影绘制到 uibezierpath

Swift draw shadow to a uibezier path


我有一个奇怪的问题。尽管我确实阅读了很多关于如何执行此操作的教程,但最终结果只显示了贝塞尔线,没有任何阴影。我的代码非常简单:

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是frame的宽和高。
非常感谢!

我直接从我的 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)

我更喜欢添加阴影子层的方式。您可以轻松使用以下功能 (Swift 3.0):

func createShadowLayer() -> CALayer {
    let shadowLayer = CALayer()
    shadowLayer.shadowColor = UIColor.red.cgColor
    shadowLayer.shadowOffset = CGSize.zero
    shadowLayer.shadowRadius = 5.0
    shadowLayer.shadowOpacity = 0.8
    shadowLayer.backgroundColor = UIColor.clear.cgColor
    return shadowLayer
}

最后,你只需将它添加到你的线路径 (CAShapeLayer):

let line = CAShapeLayer()
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 50, y: 100))
path.addLine(to: CGPoint(x: 100, y: 50))
line.path = path.cgPath
line.strokeColor = UIColor.blue.cgColor
line.fillColor = UIColor.clear.cgColor
line.lineWidth = 2.0
view.layer.addSublayer(line)

let shadowSubLayer = createShadowLayer()
shadowSubLayer.insertSublayer(line, at: 0)
view.layer.addSublayer(shadowSubLayer)

我正在使用形状图层的阴影属性为其添加阴影。这种方法最好的部分是我不必明确提供路径。阴影跟随层的路径。我还通过更改路径为图层设置动画。在那种情况下,阴影也无需一行代码即可无缝动画。

这是我正在做的 (Swift 4.2)

    shapeLayer.path = curveShapePath(postion: initialPosition)
    shapeLayer.strokeColor = UIColor.clear.cgColor
    shapeLayer.fillColor = shapeBackgroundColor
    self.layer.addSublayer(shapeLayer)

    if shadow {
        shapeLayer.shadowRadius = 5.0
        shapeLayer.shadowColor = UIColor.gray.cgColor
        shapeLayer.shadowOpacity = 0.8
    }

curveShapePath方法是returns路径的方法,定义如下:

func curveShapePath(postion: CGFloat) -> CGPath {
    let height: CGFloat = 37.0
    let path = UIBezierPath()

    path.move(to: CGPoint(x: 0, y: 0)) // start top left
    path.addLine(to: CGPoint(x: (postion - height * 2), y: 0)) // the beginning of the trough

    // first curve down
    path.addCurve(to: CGPoint(x: postion, y: height),
                  controlPoint1: CGPoint(x: (postion - 30), y: 0), controlPoint2: CGPoint(x: postion - 35, y: height))
    // second curve up
    path.addCurve(to: CGPoint(x: (postion + height * 2), y: 0),
                  controlPoint1: CGPoint(x: postion + 35, y: height), controlPoint2: CGPoint(x: (postion + 30), y: 0))

    // complete the rect
    path.addLine(to: CGPoint(x: self.frame.width, y: 0))
    path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
    path.addLine(to: CGPoint(x: 0, y: self.frame.height))
    path.close()

    return path.cgPath
}