ios - 如何在 tabbarcontroller 中旋转一个 View ?

标签 ios objective-c rotation

我觉得我的问题在 iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates) 中得到了解答,但我还不够了解如何实现响应。

我正在使用 iOS6 编写一个选项卡式应用程序,我希望其中一个 View Controller 旋转,而其余 View Controller 保持不动。我知道我需要使用 supportedInterfaceOrientations,但我不知道如何或在何处使用它,而且我无法破译有关它的 Apple 文档。 UITabBarController 被设置为 AppDelegate 中的 Root View Controller ,如果相关的话。

在此先感谢您的帮助。

最佳答案

这个问题困扰了我一个星期。我遇到了完全相同的情况,只希望也能旋转一个 View 。在选项卡式应用程序中绕过它的方法是注册方向通知。在您的 TabController(和任何其他 super View )和您要旋转的 VC 中为 shouldAutoRotate 返回 NO。然后在 viewWillAppear 中注册通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification
          object:[UIDevice currentDevice]];

被调用的函数将如下所示:

- (void) orientationChanged:(NSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
            break;

        case UIDeviceOrientationPortraitUpsideDown:
            break;
        case UIDeviceOrientationLandscapeLeft:
            if(viewHasAppeared==true){
                [self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
            break;
        case UIDeviceOrientationLandscapeRight:
            if(viewHasAppeared==true){
                [self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
            break;
        default:
            break;
    };
}

-在 ViewDidAppear 中放置一个 bool 值,因为在 View 实际出现之前您无法继续。

现在,如果你在横向模式下切换到的 View 不是选项卡式 View Controller ,那么最好的处理方法是只允许在该 View 中进行自动旋转,然后在 willAnimate 中你只需关闭 VC,如下所示:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
       [self dismissViewControllerAnimated:YES completion:nil];     
    }
}

如果它是一个选项卡式 View ,那么也只需在该 View 中注册方向更改。但是,请确保在离开 View 时删除 orientationChange 通知。如果你不这样做,那么你也会从其他 viewControllers 切换到横向。

当您离开去另一个选项卡时,您需要实现 tabBarController 委托(delegate)并确保像这样移除观察者:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if(![NSStringFromClass([viewController class])isEqualToString:@"FirstViewController"])
    {
            [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];             
    }

}

关于ios - 如何在 tabbarcontroller 中旋转一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467508/

相关文章:

javascript - 如何围绕中心旋转 HTML5 Canvas 上的图像

c++ - 从 3D 空间中的 2 个点计算四元数/旋转

jquery - 使用 jQuery 更改 CSS 旋转值

iphone - 如何像 UITableView 一样重新排序 UIScrollView 行

ios - Ionic Popover 适用于 XCode 模拟器,但不适用于 iPhone

objective-c - PHP 的 "Variable Variables"的 Objective C 等价物

ios - 是否可以从共享扩展执行 API 调用

ios - iOS 13 中的 viewDidAppear 问题

ios - 如何制作密码确认器 swift 3

Objective-C/Cocoa 等效于 C# ManualResetEvent