禁用方向更改旋转动画

Arl*_*son 25 iphone animation objective-c orientation ios

我需要禁用方向更改时播放的动画.这可能吗?如果没有,是否可以加快速度?

只是为了澄清,我不想停止方向改变,只是动画.我希望立即改变方向.

Nil*_*nch 48

是的,可以禁用动画,而不会破坏一切.

以下代码将禁用"黑匣子"旋转动画,而不会弄乱其他动画或方向代码:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [UIView setAnimationsEnabled:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    [UIView setAnimationsEnabled:NO];
  /* Your original orientation booleans*/

    return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

把它放在你的UIViewController中,一切都很好.相同的方法可以应用于iOS中的任何不需要的动画.

祝你的项目好运.


对于2016年,似乎shouldAutorotateToInterfaceOrientation无法覆盖.以下似乎确实有效,并且不会造成其他伤害.

// these DO SEEM TO WORK, 2016
override func willRotateToInterfaceOrientation(
        toInterfaceOrientation:UIInterfaceOrientation, duration:NSTimeInterval)
    {
    UIView.setAnimationsEnabled(false)
    super.willRotateToInterfaceOrientation(toInterfaceOrientation,duration:duration)
    }
override func didRotateFromInterfaceOrientation(
        fromInterfaceOrientation:UIInterfaceOrientation)
    {
    super.didRotateFromInterfaceOrientation(fromInterfaceOrientation)
    UIView.setAnimationsEnabled(true)
    }
Run Code Online (Sandbox Code Playgroud)

然而!!实际上这两个功能都已被弃用. viewWillTransitionToSize现在照顾"之前"和"之后".

override func viewWillTransitionToSize(size:CGSize,
       withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
    {
    coordinator.animateAlongsideTransition(nil, completion:
        {_ in
        UIView.setAnimationsEnabled(true)
        })
    UIView.setAnimationsEnabled(false)
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator);
    }
Run Code Online (Sandbox Code Playgroud)

  • @Lawson - FWIW,现已弃用.看编辑! (2认同)

ada*_*amz 12

在iOS8中,您可以通过在视图控制器的实现中禁用CALayer动画来禁用旋转动画 viewWillTransitionToSize.

如果要自定义转换,则接受的答案中提到的反向旋转非常有用(请参阅WWDC 2014'在iOS 8中查看控制器进度'),但如果您只想阻止动画则不行.

(NB实现viewWillTransitionToSize将阻止调用已弃用的iOS8之前的旋转API.)

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        // You could make a call to update constraints based on the
        // new orientation here.
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [CATransaction commit];
    }];
}
Run Code Online (Sandbox Code Playgroud)


Bru*_*ino 5

根据 Nils Munch 的回答,这是 Swift 3 版本:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate(alongsideTransition: nil) { (_) in
        UIView.setAnimationsEnabled(true)
    }
    UIView.setAnimationsEnabled(false)
    super.viewWillTransition(to: size, with: coordinator)
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*gan 2

如果我没记错的话(这总是我必须花点时间才能弄明白的事情)willRotateToInterfaceOrientation:duration:并且willAnimateRotationToInterfaceOrientation:duration:都在动画块内,而didRotateFromInterfaceOrientation:在动画块之后运行。我相信您需要布置您的视图,以便它们出现在旋转后它们不旋转willRotate时出现的位置(如果这有意义的话)。这样,动画就会以与设备旋转相反的方向将它们从原始(正确)布局动画化到新(旋转)布局,从而创建无动画的外观。旋转完成后,您可以在 中布置视图(不带动画),给人一种它们立即旋转的印象。didRotate

清澈如泥,不是吗?