简体   繁体   English

如何在AutoRotation期间抑制动画

[英]How do I suppress animation during AutoRotation

I'm making an app where one of the objects needs to appear not to move or rotate during AutoRotation, but everything else needs to rotate and be repositioned. 我正在创建一个应用程序,其中一个对象需要在AutoRotation期间不显示移动或旋转,但其他所有内容都需要旋转并重新定位。 The way I did this is by manually rotating the object, and moving it into the same position, relative to the device, that it had prior to rotation. 我这样做的方法是手动旋转物体,并将其移动到相对于装置的相同位置,即旋转前的位置。 The problem is that it looks funny, appearing to rotate into the same position it had before. 问题是它看起来很有趣,似乎旋转到它以前的相同位置。

If I suppress the animation effects during rotation, it would appear that the object never moved, and everything else just snapped into place, which is what I want. 如果我在旋转过程中抑制动画效果,那么看起来对象永远不会移动,而其他所有东西都会被捕捉到位,这就是我想要的。 I haven't been able to find anything in the documentation that tells me how to do this though. 我无法在文档中找到任何告诉我如何执行此操作的内容。 How do I do this? 我该怎么做呢?

When it comes to UIViewController rotation there are two ways you can set up so you get a chance to rotate and move your own views. 当谈到UIViewController旋转时,有两种方法可以设置,这样你就有机会旋转并移动自己的视图。

In the one-step process, in your UIViewController-derived class provide an implementation of willAnimateRotationToInterfaceOrientation . 在一步过程中,在您的UIViewController派生类中提供了willAnimateRotationToInterfaceOrientation的实现。 It gets called and you can kick off your object rotation so while the system is rotating everything else your object is getting counter-rotated so it looks like it's staying put. 它被调用,你可以开始你的对象旋转,所以当系统旋转所有其他对象正在反向旋转,所以看起来它保持不变。 You'll want to calculate how much to counter-rotate and in which direction based on current interface orientation vs. new orientation. 您需要根据当前界面方向与新方向计算反向旋转的数量和方向。

The other way is to get notified to do custom rotation in two steps. 另一种方法是通知两个步骤进行自定义轮换。 For example, in the first half you can shrink the object down, move it, and rotate it part way then in the second half finish the rotation as you scale back up to normal size. 例如,在上半部分中,您可以缩小对象,移动它,然后将其部分旋转,然后在后半部分完成旋转,同时缩小到正常尺寸。 It's a pretty clever way to make the rotation animation look smoother to the eye. 这是一种非常聪明的方法,可以让旋转动画看起来更加流畅。

For the two-step process, you need to define two methods. 对于两步过程,您需要定义两个方法。 willAnimateFirstHalfOfRotationToInterfaceOrientation gets called for the first half of the rotation (ie up to 45 degrees for a 90 degree rotation and at 90 degrees for an upside down flip). willAnimateFirstHalfOfRotationToInterfaceOrientation在旋转的前半部分被调用(即,对于90度旋转高达45度,对于倒置翻转高达90度)。 Once past that point the second half is called via willAnimateSecondHalfOfRotationFromInterfaceOrientation . 一旦超过该点,后半部分将通过willAnimateSecondHalfOfRotationFromInterfaceOrientation

If your object has a 1:1 aspect ratio (ie square or round) and in the middle of the view then the one-step process will probably work fine. 如果您的对象具有1:1的宽高比(即正方形或圆形)并且在视图的中间,那么一步过程可能会正常工作。 But if it's a non-square object and has to move position (for example if it's at position 40, 60 in portrait but moves to 20, 100 in landscape) and maybe even needs a bit of scaling to look better then you may want to try the two-step process and see if it looks smoother. 但是如果它是一个非方形物体并且必须移动位置(例如,如果它在位置40,60在纵向但移动到20,100在风景中)甚至可能需要一些缩放看起来更好然后你可能想要尝试两步过程,看看它是否看起来更顺畅。

If your object is inside its own individual UIView then it's pretty easy to schedule the rotations through UIView animations. 如果您的对象位于其自己的UIView中,那么通过UIView动画安排旋转非常容易。 Just create a transform through CGAffineTransformMakeRotation , then inside a pair of UIView beginAnimations / commitAnimations blocks set the transform property of the view to this value. 只需通过CGAffineTransformMakeRotation创建一个变换,然后在一对UIView beginAnimations / commitAnimations块中将视图的transform属性设置为该值。 You can tweak the timing through setAnimationDuration . 您可以通过setAnimationDuration调整时序。

EDIT: Based on the comments, I'm adding some code to show how you could attach the view to the top-level window instead of to the view controller. 编辑:根据评论,我添加一些代码,以显示如何将视图附加到顶级窗口而不是视图控制器。 Your object would then reside in this view instead of the one managed by controller (which is getting rotated). 然后,您的对象将驻留在此视图中,而不是由控制器管理的对象(正在旋转)。 You still need to over-ride the UIViewController rotate methods, but instead of rotating an object under control of the view controller you would trigger a counter-rotation in the object on the top-level. 您仍然需要覆盖UIViewController旋转方法,但不是在视图控制器的控制下旋转对象,而是在顶层的对象中触发反向旋转。

To add a view to the top-level window: 要将视图添加到顶级窗口:

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

Keep a reference to yourView somewhere you can get to, then in the UIViewController's willAnimateRotationToInterfaceOrientation counter-rotate yourView, ie calculate how much to rotate the view in reverse to where you're going--if the phone is turning 90-degrees clockwise, you'll want to rotate the view back 90 degrees counter-clockwise, etc. Then use UIView animations on yourView . 在你可以到达的某个地方保留对你的yourView的引用,然后在UIViewController的willAnimateRotationToInterfaceOrientation中反向旋转你的视图,即计算将视图反向旋转多少到你去的地方 - 如果手机顺时针旋转90度,你我想将视图逆时针旋转90度等等。然后在你的yourView上使用UIView动画。

If you don't want rotation animations, overload -shouldAutorotateToInterfaceOrientation: in your UIViewController and return NO . 如果你不想要旋转动画,请在你的UIViewController中重载-shouldAutorotateToInterfaceOrientation:并返回NO You can then observe UIDeviceOrientationDidChangeNotification to receive manual orientation change notifications. 然后,您可以观察UIDeviceOrientationDidChangeNotification以接收手动方向更改通知。

This may work: 这可能有效:

  1. Listen for willRotateToInterfaceOrientation 听取willRotateToInterfaceOrientation
  2. [UIView setAnimationsEnabled:NO]; [UIView setAnimationsEnabled:NO];
  3. Listen for UIDeviceOrientationDidChangeNotification 听取UIDeviceOrientationDidChangeNotification
  4. [UIView setAnimationsEnabled:YES]; [UIView setAnimationsEnabled:YES];

Untested, but I know that does work for some cases. 未经测试,但我知道这对某些情况有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM