Swift draws shadows into a UIBezier path

I have a weird question. Although I did read a lot of tutorials on how to do this, the end result only shows the Bezier line, not any shadows. I The code is very 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 because I don’t seem to see any errors but of course I am wrong because no shadow appears. The function is drawRect, and the basic UIVIew doesn’t have any additional things, x and y are the width and height of the frame. Thanks in advance!

I get this example directly from my PaintCode application. Hope this helps.

//// General Declarations
let context = UIGraphicsGetCurrentContext()


//// Shadow Declarations< br />let shadow = UIColor.blackColor()
let shadowOffset = CGSizeMake(3.1, 3.1)
let shadowBlurRadius: CGFloat = 5

//// Bezier 2 Drawing< br />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)

I have a weird question. Although I did read a lot of tutorials on how to do this, the final result only showed Bei Celerian lines, not any shadows. My code is very 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)

I did something wrong because I didn’t seem to see any errors but of course I was wrong because no shadow appeared. The function is drawRect, the basic UIVIew has nothing extra, x and y are the width and height of the frame. Thank you in advance!

I got this example directly from my PaintCode application. Hope this helps.

//// General Declarations
let context = UIGraphicsGetCurrentContext()


//// Shadow Declarations
let shadow = UIColor.blackColor()< br />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)

Leave a Comment

Your email address will not be published.