CATransformLayer用来创建真是的3D层次视图,而不是像其他的CALayer通过扁平的层次结果去渲染视图模型.
不同于普通的CALayer中子图层的z轴是0, CATransformLayer的子图层Z轴不为0,因此有很大普通CALayer的特性CATransformLayer并不支持.
-
只有子图层被渲染,CALayer的很多特性被忽略,包括背景颜色,contents,边框设置,绘制样式属性等.
-
默认的二维图片的处理也被忽略,包括 filters, backgroundFilters, compositingFilter, mask, masksToBounds, and shadow style properties.
-
透明度属性只是作用于单独的图层,并不作用于整个图层组.
-
hitTest方法永远都不会被调用,因为hitTest是基于二维坐标系无法映射到三维坐标系.
效果图中的第一个图是通过CATransformLayer添加子视图的结果,第二个是CALayer添加自视图的结果,背景的Layer都设置了m34(景深)设置.
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
网友评论