How to find X, Y and Rotation value of a UIView after CATransform3D applied

1.1k views Asked by At

I have a UIView on my screen. I am applying layer.transform to that view with translation and rotation according to users tap movement using tap and rotation gesture. At last i want to retrieve the final x and y position with the rotation separately. Could not find any such post here to get those information from transform. Can anyone help with this?

Here is the code am using to apply the transform.

var transform = CATransform3DIdentity
transform = CATransform3DTranslate(transform, displacementX, displacementY, 1.0)
transform = CATransform3DRotate(transform, gesture.rotation, 0, 0, 1.0)

self.currentItem.imageView.layer.transform = transform
1

There are 1 answers

3
Manikanta Chintapalli On

Please refer the following code,

For Applying Transform,

let degrees = 90.0
let radians = CGFloat(degrees * Double.pi / 180)
sampleView.layer.transform = CATransform3DMakeRotation(radians, 0.0, 0.0, 1.0)

To get rotation angle after transform,

let radiansFromSampleView = atan2(sampleView.transform.b, sampleView.transform.a)
let DegreesFromRadiansOFSampleView = CGFloat(180 * Double(radiansFromSampleView) / Double.pi)

For x and y positions you can directly take from frame of the view even after transformation.

Hope this can be helpful.