Scale an image and then rotate it?

0

I have a png image on an object. The image is a circle filled with a color. I want to scale the object higher than wide so that the circle looks like an oval. When trying to scale the object it works fine but then if I want to rotate the oval an angle does not do it to me.

If instead I do the procedure first rotate and then climb it does it well. Why do I have to rotate before I climb?

The code that does not work is the following:

CGAffineTransform transform = tempObjeto.transform;
transform = CGAffineTransformScale(transform, escalaAncho, escalaAlto);
transform = CGAffineTransformRotate(transform, angulo);
tempObjeto.transform = transform;

And the code that works is this:

CGAffineTransform transform = tempObjeto.transform;
transform = CGAffineTransformRotate(transform, angulo);
transform = CGAffineTransformScale(transform, escalaAncho, escalaAlto);
tempObjeto.transform = transform;
    
asked by Popularfan 07.02.2017 в 00:25
source

1 answer

2

I tried both codes and both make the transformations, but you have to be very careful because the order in which you place the transformations affect the final result.

Notice also that the transformation of rotation is done in radians. You may be entering a closed angle such as 90º (M_PI / 2) or 180º (M_PI). The easiest way to prove it, is to put the transformation in an animation block and see the movement.

[UIView animateWithDuration:2.0f animations:^{
    tempObjeto.transform = transform;
}];

If you want to experience the results in the order of the transformations, I leave this code that performs the transformation and prints the final dimensions. It is a matter of changing the order of rotation and scaling and check what the console throws at you.

UIView* targetView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
targetView.backgroundColor = [UIColor redColor];
[self.view addSubview:targetView];

CGAffineTransform transform = targetView.transform;
transform = CGAffineTransformRotate(transform, 0.5f);
transform = CGAffineTransformScale(transform, 1.2f, 1.5f);
targetView.transform = transform;

NSLog(@"frame %@ bounds %@", NSStringFromCGRect(targetView.frame), NSStringFromCGRect(targetView.bounds));

UIView* backgroundView = [[UIView alloc] initWithFrame:targetView.frame];
backgroundView.backgroundColor = [UIColor yellowColor];
[self.view insertSubview:backgroundView atIndex:0];

I hope I have helped you.

    
answered by 22.02.2017 / 16:23
source