Inconsistencies between CGAffineTransformRotate and M_PI (Objective-C, iOS)

I'm trying to make simple twists and turns in Objective-C, and there are a couple of problems right away. One thing is the mismatch that I get from CGAffineTransformRotate, and how the M_PI is incompatible when used inside this function.

Suppose I do this and attach to a button. When I click it once, it rotates 180 degrees counterclockwise (well and the documentation follows), but when I click it again, it rotates 180 degrees clockwise, even if the value is not negative. Changing M_PI to -M_PI does the same without any difference in rotation:

[UIView animateWithDuration:secs delay:0.0 options:option animations:^{ self.transform = CGAffineTransformRotate(self.transform, M_PI); //Inconsistent } completion:nil]; 

Now suppose I changed the M_PI to 3.141593, that is, the value that the M_PI contains when I print it. Now when I press the button, it works perfectly fine. Both times, it will rotate 180 degrees counterclockwise. When I change it to -3.141593, it will also work perfectly fine clockwise:

 self.transform = CGAffineTransformRotate(self.transform, 3.141593); //Works 

When I play with it more, the behavior becomes weirder.

Suppose I want to rotate 90 degrees (pi / 2). M_PI now has the same behavior as using a value, but the rotation is the opposite of what it should be:

 //Should be Clockwise but rotates CounterClockwise self.transform = CGAffineTransformRotate(self.transform, -M_PI/2); self.transform = CGAffineTransformRotate(self.transform, -1.5707965); //Should be CounterClockwise but rotates Clockwise self.transform = CGAffineTransformRotate(self.transform, M_PI/2); self.transform = CGAffineTransformRotate(self.transform, 1.5707965); 

And if I want to rotate something more than 180 degrees (PI), then I just rotate the shortest route, even if I indicate a positive or negative rotation. When I rotate 360 โ€‹โ€‹degrees (2PI), it doesn't even rotate.

Why is this happening, and what can I do to make it more consistent? And the second question I have is how can I rotate things 270 and 360 degrees.

+4
source share
2 answers

The problem is that you cannot control the direction of rotation when using animateWithDuration. He will always have the shortest route. When turning 180 degrees, the behavior is not defined, since in theory there are 2 possibilities for the shortest route. This also explains why you cannot rotate more than 180 degrees. If you want more control over the animation or perform more complex animations, use CAKeyframeAnimation.

+11
source

As phix23 said: always takes the shortest route. Thus, you can avoid this by using this trick, for example, to walk in a circle clockwise: you must divide the circle into, for example, 3 control points:

 [UIView animateWithDuration:secs delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ self.transform = CGAffineTransformRotate(self.transform, M_PI_2/3); } completion:(BOOL end) { [UIView [UIView animateWithDuration:secs delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ self.transform = CGAffineTransformRotate(self.transform, -M_PI_2/3); } completion:(BOOL end) { } } ]; 

This code has not been tested, so if there is a problem, try playing with it. Hope this helps.

0
source

Source: https://habr.com/ru/post/1438471/


All Articles