Instance Method

convert(_:from:)

Converts the point from the specified layer’s coordinate system to the receiver’s coordinate system.

Declaration

func convert(_ p: CGPoint, from l: CALayer?) -> CGPoint

Parameters

p

A point specifying a location in the coordinate system of l.

l

The layer with p in its coordinate system. The receiver and l and must share a common parent layer. This parameter may be nil.

Return Value

The point converted to the receiver’s coordinate system.

Discussion

If you specify nil for the l parameter, this method returns the original point subtracted from the layer's frame's origin.

Listing 1 shows code that creates two layers, redLayer and yellowLayer. yellowLayer is scaled so that it is half of its original size.

Listing 1

Creating layers with different coordinate systems

let layerFrame = CGRect(x: 0, y: 0, width: 640, height: 480)
     
let redLayer = CALayer()
redLayer.frame = layerFrame
redLayer.backgroundColor = UIColor.red.cgColor
     
let yellowLayer = CALayer()
yellowLayer.frame = layerFrame
yellowLayer.backgroundColor = UIColor.yellow.cgColor
yellowLayer.transform = CATransform3DMakeScale(0.5, 0.5, 1)

Figure 1 shows the two layers and an overlaid point (rendered as a blue cross) with a position of (50.0, 50.0) in the red layer's coordinate system.

Figure 1

Layers with different coordinate systems

Layers with different coordinate systems

Listing 2 show how you can find the coordinates of that point in the yellow layer's coordinate system.

Listing 2

Converting between layer coordinate systems

let position = CGPoint(x: 50, y: 50)
print(yellowLayer.convert(position, from: redLayer)) // prints (-220.0, -140.0)

See Also

Mapping Between Coordinate and Time Spaces

func convert(CGPoint, to: CALayer?) -> CGPoint

Converts the point from the receiver’s coordinate system to the specified layer’s coordinate system.

func convert(CGRect, from: CALayer?) -> CGRect

Converts the rectangle from the specified layer’s coordinate system to the receiver’s coordinate system.

func convert(CGRect, to: CALayer?) -> CGRect

Converts the rectangle from the receiver’s coordinate system to the specified layer’s coordinate system.

func convertTime(CFTimeInterval, from: CALayer?) -> CFTimeInterval

Converts the time interval from the specified layer’s time space to the receiver’s time space.

func convertTime(CFTimeInterval, to: CALayer?) -> CFTimeInterval

Converts the time interval from the receiver’s time space to the specified layer’s time space