How to suppress animation during AutoRotation

I am creating an application in which one of the objects should appear in order not to move or rotate during AutoRotation, but everything else should rotate and move. The way I did it was to manually rotate the object and move it in the same position relative to the device that it had before the rotation. The problem is that he looks funny, appearing in the same position as before.

If I suppress the effects of animation during rotation, it would seem that the object never moved, and everything else just fell into place, what I want. I could not find anything in the documentation that tells me how to do this. How to do it?

+4
source share
3 answers

When it comes to rotating a UIViewController, you can tweak two ways so you can rotate and move your own views.

In a one-step process, an implementation of willAnimateRotationToInterfaceOrientation provided in your UIViewController-derived class. It is called, and you can start rotating your object, so that when the system rotates everything else, your object rotates as an enemy, so it looks like it remains in place. You want to calculate how much for the opposite rotation and in which direction based on the current orientation of the interface and the new orientation.

Another way is to receive a notification about the execution of arbitrary rotation in two stages. For example, in the first half, you can squeeze an object down, move it and partially rotate it, and then in the second half, end the rotation while you zoom out to normal size. This is a pretty smart way to make the rotation animation more smoothly with the eye.

For a two-step process, you need to define two methods. willAnimateFirstHalfOfRotationToInterfaceOrientation is called for the first half of the rotation (i.e. up to 45 degrees for a 90 degree rotation and at 90 degrees for an inverted somersault). After this point, the second half is called through willAnimateSecondHalfOfRotationFromInterfaceOrientation .

If your object has a 1: 1 aspect ratio (i.e., square or round), and is in the middle of the view, then the one-step process is likely to work fine. But if it is a non-square object and it needs to move the position (for example, if it is at position 40, 60 in the portrait, but moves 20, 100 into the landscape) and may even require a bit of scaling to look better, then you might want to try the two-step process and see if it looks smoother.

If your object is inside its own individual UIView, then it's pretty easy to schedule a rotation through the UIView animation. Simply create a transformation using CGAffineTransformMakeRotation , then inside the pair of UIView blocks beginAnimations / commitAnimations set the transform property of the view to this value. You can set the time using setAnimationDuration .

EDIT:. Based on the comments, I add code to show how you can attach the view to the top-level window, and not to the view controller. Then your object will be in this view instead of being controlled by the controller (which rotates). You still need to override the rotation methods of the UIViewController, but instead of rotating the object under the control of the view controller, you cause a counter rotation in the object at the top level.

To add a view to the top-level window:

 YourAppDelegate* windowDelegate = ((YourAppDelegate*) [UIApplication sharedApplication].delegate); [windowDelegate.window addSubview:yourView]; 

Keep a link to yourView somewhere that you can get to, and then in the UIViewController willAnimateRotationToInterfaceOrientation meet rotation around yourView, that is, calculate how many reasons to view in the opposite direction to where you are going - if the phone is rotated 90 degrees clockwise arrow, you want to rotate the view back 90 degrees counterclockwise, etc. Then use the UIView animation on yourView .

+1
source

If you don't want rotation animations, overload -shouldAutorotateToInterfaceOrientation: into your UIViewController and return NO . You can then watch the UIDeviceOrientationDidChangeNotification to receive notifications of a change in manual orientation.

0
source

This may work:

  • Listen to willRotateToInterfaceOrientation
  • [UIView setAnimationsEnabled: NO];
  • Listen to UIDeviceOrientationDidChangeNotification
  • [UIView setAnimationsEnabled: YES];

Unconfirmed, but I know this works in some cases.

0
source

All Articles