Open churabou opened 6 years ago
viewに円を描く
class CircleView: UIView {
override func draw(_ layer: CALayer, in ctx: CGContext) {
UIGraphicsPushContext(ctx)
let path = UIBezierPath(ovalIn: CGRect(0, 0, 200, 200))
UIColor.blue.setFill()
path.fill()
UIGraphicsPopContext()
}
override func draw(_ layer: CALayer, in ctx: CGContext) {
ctx.addEllipse(in: bounds)
ctx.setFillColor(UIColor.blue.cgColor)
ctx.fillPath()
}
override func draw(_ rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()!
ctx.addEllipse(in: bounds)
ctx.setFillColor(UIColor.blue.cgColor)
ctx.fillPath()
}
override func draw(_ rect: CGRect) {
let path = UIBezierPath(ovalIn: bounds)
UIColor.blue.setFill()
path.fill()
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
let orange = CircleView()
orange.layer.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
view.addSubview(orange)
// orange.setNeedsDisplay()
orange.layer.setNeedsDisplay()
}
質問
let orange = UIView()
orange.layer.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
view.addSubview(orange)
orange.layer.backgroundColor = UIColor.orange.cgColor
orange.layer.setNeedsDisplay()//再描画でlayerが黒くなる
setNeedLayoutでlayoutsubviewを呼ぶ
or