コードは呼び出しを必要とせず、CGContextSetRGBFillColor
呼び出しがありませんCGContextStrokeRect
。Swift 5を使用すると、最終的なdraw(_:)
実装は次のようになります。
class CustomView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 0.5)
let rectangle = CGRect(x: 0, y: 100, width: 320, height: 100)
ctx.stroke(rectangle)
}
}
別の方法として、本当に電話をかけたい場合はCGContextSetRGBFillColor
、も電話してくださいCGContextFillRect
。Swift 3を使用すると、最終的なdraw(_:)
実装は次のようになります。
class CustomView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
ctx.setFillColor(red: 1, green: 1, blue: 1, alpha: 0)
ctx.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 0.5)
let rectangle = CGRect(x: 0, y: 100, width: 320, height: 100)
ctx.fill(rectangle)
ctx.stroke(rectangle)
}
}