Rotating UIImageView - ios

I want to rotate a UIImageView, and I am using this code:
-(IBAction)rotateImageView:(id)sender{
photoView.transform = CGAffineTransformMakeRotation(M_PI); //rotation in radians
}
The code works. when I press the button, the image rotates 180 degrees, but if I press it again it doesn't rotate back to its original position, but it stays still. why??

Because you are applying the same transform to the view again, without taking into account any existing transform.
Try this:
photoView.transform = CGAffineTransformRotate(photoView.transform,M_PI);

you have to rotate the imageView from the current rotation to make it original
use this code
CGAffineTransformRotate(photoView.transform, M_PI);

Related

Zooming is missing after rotating the image

I used pinch gesture for zooming. It's working fine. Using slider I am rotating that image. Rotation is also working perfect.
The problem after zooming, if I rotate the image, the zoom effect is missing. Here is the code I used for rotation
subView.transform = CGAffineTransformMakeRotation(slider.value * 2*M_PI / slider.maximumValue);
Thanks in Advance.
What is zooming? It is a transform. That’s all it is.
So your code comes along and rips out that transform and replaces it with a different transform.
// subView has zoom transform
subView.transform = CGAffineTransformMakeRotation(slider.value * 2*M_PI / slider.maximumValue);
// zoom transform overwritten
So you have wiped out the zoom.

Set transform of view as another view

I want to set a transform of a view as another view. For example I have UIImageView and I want to have the same transform in another UIImageView like in the first one.
I try to make it by using CGAffineTransformMakeRotation, but I don't know how to get the relative angle of the first UIImageView, to make the rotation. Maybe it can be done in different way?
I made a picture to explain the effect which I want to get:
Best regards,
EDIT: I want to set second one in the same position like the first one. Not in the same place, but with the same angle for example to the border of screen.
It's simply just need to assign the transform of one view to other.
eg.
self.imageView1.transform = CGAffineTransformMakeRotation( 45.0/180*M_PI );
self.imageView2.transform = self.imageView1.transform;
imageView2 will same transform as ImageView1.
okay, u can get the transform of your second image view (20) marked image use that transform to set 4th image view (in your pic) for example,
CGAffineTransform currentTransform = self.imageView_2.transform; //say image view rotated transform
self.imageView_4.transform = currentTransform; //last image set its transform to any view that u want to rotate
Edit:2
i am not get what u want exactly, if u want to place it somewhere, first set the frame and after that apply transform, like
self.imageView_3.frame = self.imageView_1.frame; //set the place where u want to place,
CGAffineTransform currentTransform = self.imageView_2.transform;
self.imageView_3.transform = currentTransform;
edit 3
if u want to rotate back to normal then use like below
self.imageView_3.frame = self.imageView_1.frame; //set the place where u want to place,
CGAffineTransform currentTransform = self.imageView_2.transform;
self.imageView_3.transform = CGAffineTransformIdentity;//currentTransform; //brings back to original
//set the position where u want to place
Here using property transform and method CGAffineTransformMakeRotation of imageView can do that.
e.g.
self.imageView.transform = CGAffineTransformMakeRotation( rotation degree in decimal /180*M_PI);
use animation block to animate accordingly.
[UIView animateWithDuration:1.0 animations:^{
self.imageView.transform = CGAffineTransformMakeRotation( 180.0/180*M_PI);
} completion:^(BOOL finished){
}];
Edit:
In order to rotate imageView same as a another one you can do this by assign transform property of rotated imageView see code below.
self.imageView2.transform = self.imageView1.transform;

Move, Scale, Rotate UIView to specific corner Points

I have a the corner points of a rectangle on the screen and want to fit an UIImageView in this rectangle.
I want it like in this picture:
What's the best way to achieve this?
I think black box is UIImageView and you're trying it to place in screen as in the picture.
You can put the imageview in storyboard, in xib or in code to right side of screen. Then rotate it. For example below code rotates the imageview 20 degress.
float degrees = 20; //the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);

check if imageview has been rotated — Objective-c

I have an imageview that are being rotated PI/4 (radians) every time it's taped.
That works fine with this code:
- (void)handleTap:(UITapGestureRecognizer *)tapRecognize
{
if (tapRecognize == tapRecognizer)
{
CGAffineTransform transform = CGAffineTransformRotate(imageview.transform, (M_PI / 4));
[imageview setTransform:transform];
}
The tapRecognizer is asigned to the imageview.
Now, I want to check if the imageview has been rotated. This is my code:
if (CGAffineTransformEqualToTransform(imageview.transform, rot45)) //rot45 is a CGAffineTransformMakeRotation variable which is set to M_PI / 4
{
NSLog("Rotated");
}
That works fine for the first tap, when it has been rotated 45°. But I want to be able to check when it has been taped two times, which means that it has been rotated 90°. And so on. I want different actions on each rotation-angle. How can I check that?
Sorry if the question is unclear
Devise a scheme to map the tag property to rotation. On every rotation update the tag value.

ios issue with 3d animations

I'm trying to 3D animate an UIImageView on X axis so it will do a 180 degrees rotation. At 90 degres I want to change the image. (that is why i "broke" the animation in 2).
Animation works well, but the problem is that the second image is drawn upside down. Any ideas what should i do for the image to be drawn normal?
Here is my code
i++;
[UIView animateWithDuration:2 animations:^{
self.imageView.layer.transform = CATransform3DMakeRotation(-M_PI/2 , 1, 0,0);
} completion:^(BOOL finished){
UIImage *bottomI = (UIImage *)[bottom objectAtIndex:i];
self.second.image =bottomI;
[UIView animateWithDuration:2 animations:^{
imageView.layer.transform = CATransform3DMakeRotation(M_PI , 1, 0,0);
} completion:^(BOOL finised){
}];
}];
What I've done (using grouped CABasicAnimation objects instead of view animations, but the same trick should work) is to rotate the view/layer the first 90 degrees, then rotate it 180 degrees without animation before starting the second half of the animation. That way, the second half of the animation actually causes the front face of the view/layer to rotate into view as if it was the back face, but not backwards.
The view/layer is edge-on to the viewer while you are switching it's image and rotating it 180 degrees, so the user can't see those changes. When it rotates the remaining 90 degrees, the new image is in place and it appears to be revealing the back side of the view/layer.
It works perfectly.
When you are 3d rotating a view, it is similar to rotating an object in real life. So, once the rotation is more than 90 degrees, it would invert the object. Which is why your second image is inverted. The simplest thing to do is to set the affine transform of the image to rotate it by 180 degrees before adding it in.
EDIT: My silliness....
What I intended was this
bottomI.orientation = UIImageOrientationDown

Resources