CATransformLayer用來創(chuàng)建真是的3D層次視圖僻爽,而不是像其他的CALayer通過扁平的層次結(jié)果去渲染視圖模型.
不同于普通的CALayer中子圖層的z軸是0夹厌, CATransformLayer的子圖層Z軸不為0南蹂,因此有很大普通CALayer的特性CATransformLayer并不支持.
只有子圖層被渲染,CALayer的很多特性被忽略赦邻,包括背景顏色囚玫,contents,邊框設(shè)置昨忆,繪制樣式屬性等.
默認(rèn)的二維圖片的處理也被忽略戚丸,包括 filters, backgroundFilters, compositingFilter, mask, masksToBounds, and shadow style properties.
透明度屬性只是作用于單獨(dú)的圖層,并不作用于整個(gè)圖層組.
hitTest方法永遠(yuǎn)都不會(huì)被調(diào)用扔嵌,因?yàn)閔itTest是基于二維坐標(biāo)系無法映射到三維坐標(biāo)系.
效果圖中的第一個(gè)圖是通過CATransformLayer添加子視圖的結(jié)果限府,第二個(gè)是CALayer添加自視圖的結(jié)果,背景的Layer都設(shè)置了m34(景深)設(shè)置.
CATransformLayer代碼:
let layer = CATransformLayer()
layer.addSublayer(layerOfColor(.red, zPosition: 20))
layer.addSublayer(layerOfColor(.green, zPosition: 40))
layer.addSublayer(layerOfColor(.orange, zPosition: 60))
var perspective = CATransform3DIdentity
perspective.m34 = -1 / 100
layer.transform = CATransform3DRotate(perspective, 0.1, 0, 1, 0)
layer.backgroundColor = UIColor.gray.cgColor
self.view.layer.addSublayer(layer)
CALayer代碼:
let layer = CALayer()
layer.addSublayer(layerOfColor(.red, zPosition: 20))
layer.addSublayer(layerOfColor(.green, zPosition: 40))
layer.addSublayer(layerOfColor(.orange, zPosition: 60))
var perspective = CATransform3DIdentity
perspective.m34 = -1 / 100
layer.transform = CATransform3DRotate(perspective, 0.1, 0, 1, 0)
layer.backgroundColor = UIColor.gray.cgColor
layer.frame = CGRect(x: 0, y: 300, width: 300, height: 300)
self.view.layer.addSublayer(layer)
func layerOfColor(_ color: UIColor, zPosition: CGFloat) -> CALayer {
let layer = CALayer()
layer.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
layer.backgroundColor = color.cgColor
layer.zPosition = zPosition
layer.opacity = 0.5
return layer
}
參考資料:
https://developer.apple.com/documentation/quartzcore/catransformlayer