iPhone Sdk Cgaffinetransform Getting the Angle of Rotation of an Object

iphone sdk CGAffineTransform getting the angle of rotation of an object

Technically you can't, because the transform can include a skew operation which turns the image into a parallelogram and the rotation angle isn't defined anymore.

Anyway, since the rotation matrix generates

 cos(x)  sin(x)   0
-sin(x) cos(x) 0
0 0 1

You can recover the angle with

return atan2(transform.b, transform.a);

Get just the rotation from a CGAffineTransform

Take the asin() or acos() of the affine member b or c.

struct CGAffineTransform {
CGFloat a;
CGFloat b;
CGFloat c;
CGFloat d;
CGFloat tx;
CGFloat ty;
};

Get the current angle/rotation/radian for a UIView?

You can do it this way...

CGFloat radians = atan2f(yourView.transform.b, yourView.transform.a); 
CGFloat degrees = radians * (180 / M_PI);

iPhoneSDK calculate Rotation angle from CATransform3D

It depends on what axis you are doing the rotation on.

Rotation about the z-axis is represented as:

a  = angle in radians
x' = x*cos.a - y*sin.a
y' = x*sin.a + y*cos.a
z' = z

( cos.a sin.a 0 0)
(-sin.a cos.a 0 0)
( 0 0 1 0)
( 0 0 0 1)

so angle should be a = atan2(transform.m12, transform.m11);

Rotation about x-axis:

a  = angle in radians
y' = y*cos.a - z*sin.a
z' = y*sin.a + z*cos.a
x' = x

(1 0 0 0)
(0 cos.a sin.a 0)
(0 -sin.a cos.a 0)
(0 0 0 1)

Rotation about y-axis:

a  = angle in radians
z' = z*cos.a - x*sin.a
x' = z*sin.a + x*cos.a
y' = y

(cos.a 0 -sin.a 0)
(0 1 0 0)
(sin.a 0 cos.a 0)
(0 0 0 1)

Rotate UIView around its center keeping its size

You're probably hitting a problem with Autolayout. You probably have constraints on the rotated view pinning it to the edges of the superview. When the transform is applied, Autolayout is updating the view's size to still fit within the superview.

You can experiment with different constraints (e.g. pinning the centre of the view to the centre of another view, and pinning the width and height to constant values) or turn Autolayout off for the rotated view, or, if these don't work or don't suit your needs, use a container view which is laid out under Autolayout, and add your rotating view to this, without using Autolayout.

This can only be done in code - you can make individual views subject to Autolayout or not by setting translatesAutoresizingMasksIntoConstraints to NO (Autolayout on) or YES (Autolayout off). You'll need to set the appropriate autoresizing masks if you switch a view from one to the other.

Rotate Image 5 degrees each time user clicks button in swift

The code will only rotate the imageView,if the image size changes,you may have missed some code.

func rotate(isForward: Bool) {
self.cropImageView.transform = self.cropImageView.transform.rotated(by: (isForward ? -1 : 1) * .pi * (5.0 / 180.0))
}


Related Topics



Leave a reply



Submit