Class

CATransformLayer

CATransformLayer objects are used to create true 3D layer hierarchies, rather than the flattened hierarchy rendering model used by other CALayer classes.

Overview

Unlike normal layers, transform layers do not flatten their sublayers into the plane at Z=0. Due to this, they do not support many of the features of the CALayer class compositing model:

  • Only the sublayers of a transform layer are rendered. The CALayer properties that are rendered by a layer are ignored, including: backgroundColor, contents, border style properties, stroke style properties, etc.

  • The properties that assume 2D image processing are also ignored, including: filters, backgroundFilters, compositingFilter, mask, masksToBounds, and shadow style properties.

  • The opacity property is applied to each sublayer individually, the transform layer does not form a compositing group.

  • The hitTest: method should never be called on a transform layer as they do not have a 2D coordinate space into which the point can be mapped.

Example: Displaying layers in 3D

Because CATransformLayer creates true 3D layer hierarchies, you can display otherwise hidden layers when applying 3D transforms.

Listing 1 shows three differently colored but identically sized layers added at the same position to layer. The blue layer is visible because it has the highest zPosition. Defining the layer's transform rotates the viewpoint in 3D space and, because layer is a CATransformLayer, all three layers are visible as illustrated in Figure 1.

Listing 1

Creating and displaying layers in 3D

let layer = CATransformLayer()
     
func layerOfColor(_ color: UIColor, zPosition: CGFloat) -> CALayer {
    let layer = CALayer()
    layer.frame = CGRect(x: 200, y: -200, width: 400, height: 400)
    layer.backgroundColor = color.cgColor
    layer.zPosition = zPosition
    layer.opacity = 0.5
    
    return layer
}
     
layer.addSublayer(layerOfColor(.red, zPosition: 20))
layer.addSublayer(layerOfColor(.green, zPosition: 40))
layer.addSublayer(layerOfColor(.blue, zPosition: 60))
     
var perspective = CATransform3DIdentity
perspective.m34 = -1 / 100
     
layer.transform = CATransform3DRotate(perspective, 0.1, 0, 1, 0)
Figure 1

Hidden layers made visible in 3D with CATransformLayer

Hidden layers made visible in 3D with CATransformLayer

However, if layer is created as a CALayer, the green and red layers, being hidden by the blue layer, are not rendered as illustrated in Figure 2.

Figure 2

Hidden layers remain hidden in 3D with CALayer

Hidden layers remain hidden in 3D with CALayer

Relationships

Inherits From

Conforms To

See Also

Advanced Layer Options

class CAScrollLayer

A layer that displays scrollable content larger than its own bounds.

class CATiledLayer

A layer that provides a way to asynchronously provide tiles of the layer's content, potentially cached at multiple levels of detail.

class CAReplicatorLayer

A layer that creates a specified number of copies of its sublayers (the source layer), each copy potentially having geometric, temporal, and color transformations applied to it.