convert(_:to:)
Converts the point from the receiver’s coordinate system to the specified layer’s coordinate system.
Declaration
func convert(_ p: CGPoint, to l: CALayer?) -> CGPointParameters
- p:
A point specifying a location in the coordinate system of
l. - l:
The layer into whose coordinate system
pis to be converted. The receiver andlmust share a common parent layer. This parameter may benil.
Return Value
The point converted to the coordinate system of layer.
Discussion
If you specify nil for the l parameter, this method returns the original point added to the layer’s frame’s origin.
The following example shows code that creates two layers, redLayer and yellowLayer. yellowLayer is scaled so that it is half of its original size.
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)The following figure 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.
[Image]
The following code shows how you can find the coordinates of that point in the yellow layer’s coordinate system.
let position = CGPoint(x: 50, y: 50)
print(redLayer.convert(position, to: yellowLayer))