Alexey K
Alexey K

Reputation: 6723

UIBezierPath not appearing

I try to draw a separator for slider, which must be at position of 1/3 of slider length. The slider body draws successfully, buy separator - not, it doesn't show.

Code is following

class RangeSliderTrackLayer:CALayer {
weak var rangeSlider:RangeSlider?

override func drawInContext(ctx: CGContext) {

    if let slider = rangeSlider {
        let cornerRadius = bounds.height * 1 / 2.0
        let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
        CGContextAddPath(ctx, path.CGPath)

        CGContextSetFillColorWithColor(ctx, UIColor.lightGrayColor().CGColor)
        CGContextAddPath(ctx, path.CGPath)
        CGContextFillPath(ctx)

        CGContextSetFillColorWithColor(ctx, UIColor.yellowColor().CGColor)
        let lowerValuePosition = CGFloat(40)
        let upperValuePosition = CGFloat(80)
        let rect = CGRect(x: lowerValuePosition, y: 0.0, width: upperValuePosition - lowerValuePosition, height: bounds.height)
        CGContextFillRect(ctx, rect)

        let separatorPath = UIBezierPath()
        var x = bounds.width / 3
        var y = bounds.height
        separatorPath.moveToPoint(CGPoint(x: x, y: y))
        separatorPath.addLineToPoint(CGPoint(x: x + 2, y: y))
        separatorPath.addLineToPoint(CGPoint(x: x + 2, y: 0))
        separatorPath.addLineToPoint(CGPoint(x: x, y: 0))
        separatorPath.closePath()
        UIColor.whiteColor().setFill()
        separatorPath.stroke()
    }


}

}

What am I doing wrong ?

Upvotes: 2

Views: 1842

Answers (1)

Rob
Rob

Reputation: 437552

You are calling setFill() but then then calling stroke(). Fill and stroke are two separate things. So, you either want:

  1. Go ahead and set the fill color with setFill(), but then call fill() instead of stroke():

    let separatorPath = UIBezierPath()
    var x = bounds.width / 3
    var y = bounds.height
    separatorPath.moveToPoint(CGPoint(x: x, y: y))
    separatorPath.addLineToPoint(CGPoint(x: x + 2, y: y))
    separatorPath.addLineToPoint(CGPoint(x: x + 2, y: 0))
    separatorPath.addLineToPoint(CGPoint(x: x, y: 0))
    separatorPath.closePath()
    UIColor.whiteColor().setFill()
    // separatorPath.stroke()
    separatorPath.fill()
    
  2. Or call stroke() like you are not, but instead of calling setFill(), instead set lineWidth and call setStroke():

    let separatorPath = UIBezierPath()
    var x = bounds.width / 3
    var y = bounds.height
    separatorPath.moveToPoint(CGPoint(x: x, y: y))
    separatorPath.addLineToPoint(CGPoint(x: x + 2, y: y))
    separatorPath.addLineToPoint(CGPoint(x: x + 2, y: 0))
    separatorPath.addLineToPoint(CGPoint(x: x, y: 0))
    separatorPath.closePath()
    // UIColor.whiteColor().setFill()
    UIColor.whiteColor().setStroke()
    separatorPath.lineWidth = 1
    separatorPath.stroke()
    

Upvotes: 1

Related Questions