三个图层被赋予相同的父级,但是要添加的第一个图层上的zPosition值没有效果 - 它保留在后面。为什么是这样?我读过的书说,子层数组中的位置可以用zPosition值覆盖。
import UIKit
import PlaygroundSupport
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
let v0 = UIView(frame: CGRect(0, 0, 500, 500))
v0.backgroundColor = .white
let v1 = UIView(frame: v0.frame)
let lay1 = CALayer()
lay1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1).cgColor
lay1.frame = CGRect(113, 111, 132, 194)
lay1.zPosition = 100
v1.layer.addSublayer(lay1)
let lay2 = CALayer()
lay2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1).cgColor
lay2.frame = CGRect(41, 56, 132, 194)
v1.layer.addSublayer(lay2)
let lay3 = CALayer()
lay3.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor
lay3.frame = CGRect(43, 197, 160, 230)
v1.layer.addSublayer(lay3)
v0.addSubview(v1)
PlaygroundPage.current.liveView = v0