Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if UIImageView has been rotated

I am rotating a UIImageView in this way:

-(IBAction)rotateImageView:(id)sender{
photoView.transform = CGAffineTransformRotate(photoView.transform, M_PI);
}

As you can see the image can either be up or down. Is there the possibility to detect if the image is up or down, in order to do something like this:

if(image is up)....
like image 820
user2014474 Avatar asked Jan 30 '13 16:01

user2014474


3 Answers

If you just want to tell if the image has been rotated using an affine transform, as your question implies, you can do this:

if (CGAffineTransformIsIdentity(photoView.transform)) { 
    // not rotated
    ...
} else {
    // rotated
    ...
}

If you want to check for a particular rotation, do this:

CGAffineTransform t = CGAffineTransformMakeRotation(M_PI);
if (CGAffineTransformEqualToTransform(photoView.transform, t)) {
    // rotated by M_PI
    ...
}

Note that the above two solutions only work if you are not applying OTHER affine transforms to the view at the same time as the rotation transforms. If you are also applying other transforms, perhaps you are better off just tracking the rotation state in a variable.

like image 139
Mathew Avatar answered Nov 04 '22 05:11

Mathew


Swift 4

if transform.isIdentity { }
like image 41
Varun Avatar answered Nov 04 '22 06:11

Varun


You can check the angle of the photoView, if it is 0.00 that means its up and if its 3 or 3.13 or any close value to it then it shows that its down. (you can log the exact value of down).

CGFloat angle = [(NSNumber *)[photoView valueForKeyPath:@"layer.transform.rotation.z"] floatValue]; NSLog(@"%f", angle);

like image 26
Ahsan Ebrahim Avatar answered Nov 04 '22 06:11

Ahsan Ebrahim