UIBezierPath没有出现

时间:2016-09-09 14:48:52

标签: ios swift core-graphics uibezierpath

我尝试为滑块绘制一个分隔符,它必须位于滑块长度的1/3处。滑块体成功绘制,购买分隔符 - 不是,它没有显示。

代码正在关注

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()
    }


}

}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在呼叫setFill(),然后呼叫stroke()。填充和中风是两个独立的事情。所以,你要么:

  1. 继续使用setFill()设置填充颜色,然后拨打fill()而不是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. 或者像你一样打电话给stroke(),而不是打电话给setFill(),而是设置lineWidth并致电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()