churabou / iOS-develop-blog

0 stars 0 forks source link

4月27日(金) #13

Open churabou opened 6 years ago

churabou commented 6 years ago

setNeedLayoutでlayoutsubviewを呼ぶ


didSet {
  label.text = ""
 imageView.loadImage(url: url)
}

or

update(model: Model) {
label.text = model.text
imageView.loadImage()
}
churabou commented 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()
    }
churabou commented 6 years ago

質問

        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が黒くなる