禁用方向改变旋转动画
我需要禁用方向改变时播放的动画。这可能吗?如果没有的话,是否可以加快速度?
只是为了澄清,我不想停止方向变化,只是停止动画。我想要立即改变方向。
I need to disable the animation that plays when the orientation changes. Is this possible? If not, is it possible to speed it up?
Just to clarify, i don't want to stop the orientation change, just the animation. I want an instant orientation change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,可以禁用动画,而无需破坏所有内容。
以下代码将禁用“黑匣子”旋转动画,而不会与其他动画或方向代码混淆:
将其放在 UIViewController 中,一切都应该很好。同样的方法可以应用于 iOS 中任何不需要的动画。
祝你的项目好运。
对于 2016 年,似乎
shouldAutorotateToInterfaceOrientation
无法被覆盖。以下似乎确实有效,并且不会造成其他伤害。但是!! 事实上,这两个函数都已被弃用。
viewWillTransitionToSize
现在可以处理“之前”和“之后”。Yes, it is possible to disable the animation, without breaking everything apart.
The following codes will disable the "black box" rotation animation, without messing with other animations or orientation code:
Place it in your UIViewController and all should be well. Same method can be applied to any undesired animation in iOS.
Best of luck with your project.
For 2016, it appears
shouldAutorotateToInterfaceOrientation
is not available to be overridden. The following does seem to work, and cause no other harm.However!! Indeed both these functions are deprecated.
viewWillTransitionToSize
now takes care of both the "before" and "after".在 iOS8 中,您可以通过在视图控制器的
viewWillTransitionToSize
实现中禁用 CALayer 动画来禁用旋转动画。如果您想要自定义过渡(请参阅 WWDC 2014 'iOS 8 中的视图控制器进展'),则已接受答案中提到的反向旋转很有用,但如果您只想阻止动画,则不适用。
(注意,实现 viewWillTransitionToSize 将阻止调用已弃用的 iOS8 之前的旋转 API。)
In iOS8 you can disable rotation animations by disabling CALayer animations in your view controller's implementation of
viewWillTransitionToSize
.The inverse rotation mentioned in the accepted answer is useful if you want to customize the transition (see WWDC 2014 'View Controller Advancements in iOS 8') but not if you just want to prevent animation.
(N.B. Implementing
viewWillTransitionToSize
will prevent the deprecated pre-iOS8 rotation APIs from being called.)根据 Nils Munch 的回答,这是 Swift 3 版本:
Based on Nils Munch's Answer, this is the Swift 3 version:
如果我没记错的话(这总是我必须花点时间才能得到正确的结果)
willRotateToInterfaceOrientation:duration:
和willAnimateRotationToInterfaceOrientation:duration:
都是在动画块内,而didRotateFromInterfaceOrientation:
在动画块之后运行。我相信您需要在willRotate
中布置视图,以便它们出现在旋转后出现的位置如果它们没有旋转(如果有意义的话) 。这样,动画就会以与设备旋转相反的方向将它们从原始(正确)布局动画化到新(旋转)布局,从而创建无动画的外观。旋转完成后,您可以在didRotate
中布置视图,无需动画,给人一种它们立即旋转的印象。清澈如泥,不是吗?
If I remember correctly (this is always the sort of thing I have to play around with for a moment to get right)
willRotateToInterfaceOrientation:duration:
andwillAnimateRotationToInterfaceOrientation:duration:
are both inside the animation block, whiledidRotateFromInterfaceOrientation:
runs after the animation block. I believe you would need to lay out your views inwillRotate
so they appear in the position in which they would appear after the rotation had they not rotated (if that makes sense). This way the animation will animate them from their original (correct) layout to the new (rotated) layout in the opposite direction that the device rotates, creating the appearance of no animation. Once the rotation is complete, you can lay out your views, without animation, indidRotate
, giving the impression that they rotate instantly.Clear as mud, no?
如果您在视图即将旋转时重新布局视图,我认为当它真正旋转时,它不会有动画。
我的意思是,为每个子视图编写一个包含新坐标、框架、位置的方法,并在
willRotateToInterfaceOrientation:duration:
或willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
中调用该方法(但这不是一个聪明的做事方式。我想知道,为什么你不需要动画?)
if you re-layout your views when the view is about to be rotated I think when it really rotates it won't have animations.
I mean, write a method with the new coordinates, frames, positions for each subview and call that method in
willRotateToInterfaceOrientation:duration:
orwillAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
(This is not a smart way of doing things though. I wonder, why do you need no-animation?)