1

在我的最后一个项目中,我在开发面向 UIViewController 的子类时使用了一些技术来创建一个替代的横向视图,例如 AutosizingMask 属性并在一些 layoutSubviews 方法中添加额外的代码。但是现在,我想让我公司的设计师通过编辑一些 XIB 文件来处理当前的项目艺术,为此我认为我需要两个 viewControllers 与他们的 XIB 文件链接,用于每个面向应用程序的视图。

所以,我确实开始阅读 iOS 文档的视图控制器编程指南:http: //developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101 -SW26 并查看了这个 Apple 示例代码 AlternateViews:http: //developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html

尽管阅读了这些文档,但我无法创建适合我所有需求的定向视图。假设我想创建一个带有背景图像、状态标签和活动指示器的定向加载视图。我会创建:

  • MyLoadingView*Base*ViewController - 这是一个保持逻辑的基类,它是 IBOutlets,它是下一个 XIB 的 fileOwner。

  • MyLoadingView*Portrait*ViewController - 它继承了上面的 MyLoadingViewBaseViewController 并在 MyLoadingViewPortraitViewController.xib 中有个性化的纵向视图

  • MyLoadingView*Landscape*ViewController - 它还继承了 MyLoadingViewBaseViewController 并在 MyLoadingViewLandscapeViewController.xib 中拥有个性化的横向视图。

我所学到的表明肖像类必须包含一个属性,该属性保留风景类的实例。

@interface MyLoadingViewPortraitViewController : MyLoadingViewBaseViewController {

    BOOL _isShowingLandscapeView;
    MyLoadingViewLandscapeViewController *_landscapeViewController;

} @end

并且当设备旋转时,纵向视图实例必须呈现横向视图实例,它具有模态视图:

- (void)orientationChanged:(NSNotification *)notification
{

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !_isShowingLandscapeView)
    {

        [self presentModalViewController:self.landscapeViewController
                                animated:NO];
        _isShowingLandscapeView = YES;

    }

    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
             _isShowingLandscapeView)
    {

        [self dismissModalViewControllerAnimated:NO];
        _isShowingLandscapeView = NO;

    }
}

这行得通,但让我们谈谈一些问题:

  1. 默认的 iPad 旋转动画不出现,视图突然交换给用户带来了不希望的体验。在尝试修复它时,我在 AlternateViews 示例代码中发现了以下几行。

    - (void)orientationChanged:(NSNotification *)notification
    {
        // We must add a delay here, otherwise we'll swap in the new view    
        // too quickly and we'll get an animation glitch
        [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
    }
    

    但是最终的旋转动画也不像使用 autosizingMasks 的视图的默认旋转。

  2. 我对使用模态视图演示感到不舒服。考虑尝试将当前定义为详细视图控制器的视图旋转到 UISplitViewController。如果详细视图实现了此代码,则在设备旋转后它将出现在拆分视图上。(也许添加为子视图可​​能是一个不同的解决方案。你们中的一些人尝试过吗?)

  3. 为什么不使用 UIViewController 的“响应视图旋转事件”方法而不是监听 UIDeviceOrientationDidChangeNotification。(当横向视图是窗口最前面的视图时,它将接收这些旋转事件方法并可以重新发送到将继续管理它的纵向视图。)

最后,我将不胜感激任何有关创建面向视图的答案、提示或示例代码。提前致谢。

4

0 回答 0