UIBezierPath 没有出现
UIBezierPath not appearing
我尝试为滑块绘制一个分隔符,它必须位于滑块长度的 1/3 的位置。 slider body绘制成功,买separator - 不,不显示。
代码如下
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()
我尝试为滑块绘制一个分隔符,它必须位于滑块长度的 1/3 的位置。 slider body绘制成功,买separator - 不,不显示。
代码如下
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()