我尝试为滑块绘制一个分隔符,该分隔符必须位于滑块长度的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()
}
}
}
我究竟做错了什么 ?
最佳答案
您要先调用setFill()
,然后再调用stroke()
。填充和描边是两个独立的东西。因此,您要么想要:
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()
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()
关于ios - UIBezierPath没有出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39414318/