8

当屏幕方向从横向变为纵向时,如何更改或禁用旋转动画,反之亦然?

4

3 回答 3

16

是的,可以禁用动画,而不会破坏所有内容。

以下代码将禁用“黑盒”旋转动画,而不会与其他动画或方向代码混淆:

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


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

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

祝你的项目好运。

于 2011-06-09T12:07:27.793 回答
8

如果您不希望您的视图控制器旋转,只需覆盖 shouldAutoRotateToInterface 视图控制器方法以针对您不想支持的任何方向返回 false ......这是一个参考

如果您只想以其他方式处理旋转,您可以在上述方法中返回 false 并像这样注册 UIDeviceOrientationDidChangeNotification

    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
       selector:@selector(handleOrientationDidChange:)
           name:UIDeviceOrientationDidChangeNotification
         object:nil];

现在,当你收到通知时,你可以用它做任何你想做的事情......

于 2010-04-29T16:05:52.883 回答
1

上面@Nils Munch 的答案是find < iOS7。对于 iOS 7 或更高版本,您可以使用:

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [UIView setAnimationsEnabled:NO];

    [coordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [UIView setAnimationsEnabled:YES];
    }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
于 2014-12-20T12:14:13.947 回答