我有一个UIBezierPath的实例,我想将笔画的颜色改为黑色以外的东西.有谁知道如何在斯威夫特这样做?
解决方法
使用
Swift 3,UIColor具有
setStroke()方法. setstroke()具有以下声明:
func setstroke()
Sets the color of subsequent stroke operations to the color that the receiver represents.
因此,您可以使用这样的setstroke():
strokeColor.setstroke() // where strokeColor is a `UIColor` instance
下面的游乐场代码显示了如何在UIBezierPath旁边使用setstroke(),以便在UIView子类中绘制一个绿色填充颜色和浅灰色笔画颜色的圆圈:
import UIKit
import PlaygroundSupport
class MyView: UIView {
override func draw(_ rect: CGRect) {
// UIBezierPath
let newRect = CGRect(
x: bounds.minX + ((bounds.width - 79) * 0.5 + 0.5).rounded(.down),y: bounds.minY + ((bounds.height - 79) * 0.5 + 0.5).rounded(.down),width: 79,height: 79
)
let ovalPath = UIBezierPath(ovalIn: newRect)
// Fill
UIColor.green.setFill()
ovalPath.fill()
// stroke
UIColor.lightGray.setstroke()
ovalPath.linewidth = 5
ovalPath.stroke()
}
}
let myView = MyView(frame: CGRect(x: 0,y: 0,width: 200,height: 300))
PlaygroundPage.current.liveView = myView