确定UIImageView是否已旋转

时间:2022-04-22 23:59:53

I am rotating a UIImageView in this way:

我正在以这种方式旋转UIImageView:

-(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)....

2 个解决方案

#1


10  

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.

请注意,只有在旋转变换的同时不将其他仿射变换应用于视图时,上述两种解决方案才有效。如果您还要应用其他变换,也许您最好只跟踪变量中的旋转状态。

#2


0  

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).

您可以检查photoView的角度,如果它是0.00表示它的向上,如果它是3或3.13或任何接近的值,那么它表明它的向下。 (您可以记录下来的确切值)。

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

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

#1


10  

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.

请注意,只有在旋转变换的同时不将其他仿射变换应用于视图时,上述两种解决方案才有效。如果您还要应用其他变换,也许您最好只跟踪变量中的旋转状态。

#2


0  

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).

您可以检查photoView的角度,如果它是0.00表示它的向上,如果它是3或3.13或任何接近的值,那么它表明它的向下。 (您可以记录下来的确切值)。

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

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