Swift draw shadow to a uibezier path

13,412

Solution 1

I take this example straight from my PaintCode-app. Hope this helps.

//// 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)

Solution 2

I prefer the way to add a shadow-sublayer. You can easily use the following function (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
}

And finally, you just add it to your line path (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)

Solution 3

I am using the shadow properties of my shape layer to add shadow to it. The best part of this approach is that I don't have to provide a path explicitly. The shadow follows the path of the layer. I am also animating the layer by changing path. In that case too the shadow animates seamlessly without a single line of code.

Here is what I am doing (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
    }

The curveShapePath method is the one that returns the path and is defined as follows:

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
}

enter image description here

Share:
13,412
Razvan Soneriu
Author by

Razvan Soneriu

Updated on June 26, 2022

Comments

  • Razvan Soneriu
    Razvan Soneriu almost 2 years


    I have a strange question. Even though I did read a lot of tutorials on how to do this, the final result only shows the bezier line, not any shadow whatsoever. My code is pretty simple :

    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)
    

    What am I doing wrong as I dont seem to see anything wrong but of course I am wrong since no shadow appears. The function is drawRect, basic UIVIew no extra anything in there, x and y are the width and height of the frame.
    Many thanks in advance!