churabou / iOS-develop-blog

0 stars 0 forks source link

4月21日(土) StackViewをコードで扱って見たら意外と手こずった。 #6

Open churabou opened 6 years ago

churabou commented 6 years ago
changing property borderColor in transform-only layer, will have no effect

       let stackView = UIStackView()
       let colors: [UIColor] = [.red, .yellow, .blue, .white]
        colors.forEach {
            let v = UIView()
            v.backgroundColor = $0
            stackView.addArrangedSubview(v)
        }

        let num = CGFloat(colors.count)
        let space = (view.bounds.width-80*num) / (num+1)

        stackView.bounds.size = CGSize(width: view.bounds.width-2*space, height: 80)
        stackView.center = view.center
        stackView.backgroundColor = .black
        stackView.distribution = .fillProportionally
        stackView.spacing = space
        stackView.axis = .horizontal
        view.addSubview(stackView)

うまくいかなかった例


        let stackView = UIStackView()

        [UIColor.red, UIColor.blue, UIColor.yellow].forEach {
            let v = UIView()
            v.backgroundColor = $0
            stackView.addArrangedSubview(v)
        }
        stackView.bounds.size = CGSize(width: 300, height: 300)
        stackView.center = view.center
        stackView.backgroundColor = .black
        stackView.distribution = .equalSpacing
        stackView.spacing = 8
        stackView.axis = .vertical
        stackView.alignment = .center
        view.addSubview(stackView)

UIImage().withRenderingMode(.alwaysTemplate)は透明度飲み色情報抜き でtintcolorで色を変えられる

churabou commented 6 years ago

class View: UIView {

    override var frame: CGRect {
        didSet {
         print("didSet \(frame)")
        }
    }
}

let v = View()
v.bounds = view.bounds
v.backgroundColor = .red
view.addSubview(v)

UIView.animate(withDuration: 0.3) {
    v.bounds.size = CGSize(width: 300, height: 300)
}

class View: UIView {

    override var frame: CGRect {
        didSet {
         print("didSet \(frame)")
        }
    }
}

        let v = View()
        print("instantiate \(v.frame)")

        v.bounds = view.bounds
        print("v.bounds = view.bounds \(v.frame)")

        v.center = view.center
        print("v.center = view.center \(v.frame)")

        v.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        print("v.frame = CGRect(x: 100, y: 100, width: 100, height: 100) \(v.frame)")

        v.bounds.size = CGSize(width: 200, height: 200)
        print("v.bounds.size = CGSize(width: 200, height: 200) \(v.frame)")

        v.transform = v.transform.scaledBy(x: 2, y: 1).translatedBy(x: -40, y: 50)
        print("v.transform = v.transform.scaledBy(x: 2, y: 1).translatedBy(x: -40, y: 50) \(v.frame)")
churabou commented 6 years ago
didSet (0.0, 0.0, 0.0, 0.0)
instantiate (0.0, 0.0, 0.0, 0.0)
v.bounds = view.bounds (-187.5, -333.5, 375.0, 667.0)
v.center = view.center (0.0, 0.0, 375.0, 667.0)
didSet (100.0, 100.0, 100.0, 100.0)
v.frame = CGRect(x: 100, y: 100, width: 100, height: 100) (100.0, 100.0, 100.0, 100.0)
v.bounds.size = CGSize(width: 200, height: 200) (50.0, 50.0, 200.0, 200.0)
v.transform = v.transform.scaledBy(x: 2, y: 1).translatedBy(x: -40, y: 50) (-130.0, 100.0, 400.0, 200.0)

storeされているframeの値と getしてくるframeの値が違う

churabou commented 6 years ago
class A {

    var test: Int {

        get {
            return self.test * 2
        }

        set {
            self.test = newValue
        }
    }
}

class B: A {

    override var test: Int {

        didSet {
            print(test)
        }
    }
}

get - set もういっかい読もう