创建 iPad 替代横向界面问题
在我的上一个项目中,在开发面向 UIViewController 子类时,我一直在使用一些技术来创建替代横向视图,例如 AutosizingMask 属性以及在某些 layoutSubviews 方法中添加其他代码。但现在,我想让我公司的设计师只需编辑一些 XIB 文件即可处理当前的项目艺术,为此,我认为我需要两个与每个面向应用程序的视图的 XIB 文件链接的 viewController。
因此,我确实开始阅读 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 - 这是一个保留逻辑的基类,它是 IBOutlet,是下一个 XIB 的文件所有者。
MyLoadingView*Portrait*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;
}
}
这可行,但让我们讨论一些问题:
默认的 iPad 旋转动画不会出现,视图会突然交换导致用户不想要的体验。 当尝试修复它时,我在 AlternateViews 示例代码中发现了以下几行。
- (void)orientationChanged:(NSNotification *)通知 { // 我们必须在这里添加延迟,否则我们将交换新视图 // 太快了,我们会遇到动画故障 [self PerformSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0]; }
但最终的旋转动画也不像使用 autosizingMasks 的视图的默认旋转那样出现。
我对使用模态视图演示感到不舒服。考虑尝试将当前定义为详细视图控制器的视图旋转到 UISplitViewController 中。如果详细视图实现此代码,则在设备旋转后,它将显示在拆分视图上。 (也许添加为子视图可能是一个不同的解决方案。你们中的一些人尝试过吗?)
为什么不使用 UIViewController“响应视图旋转事件”方法而不是侦听 UIDeviceOrientationDidChangeNotification。 (当横向视图是窗口最前面的视图时,它将接收这些旋转事件方法,并可以重新发送到纵向视图,纵向视图将继续管理它。)
最后,我将不胜感激任何用于创建的答案、提示或示例代码有导向的观点。 提前致谢。
During my last projects I have being using some techniques to create an alternative landscape view when developing my oriented UIViewController subclasses, such as AutosizingMask properties and adding additional code into some layoutSubviews methods. But now, I would like to allow my company's designer to work with the current project art just by editing some XIB files, and to do that I think I would need two viewControllers linked with their XIB file for each application oriented view.
So, I did start reading the View Controller Programming Guide for iOS document: http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW26
and looked at this Apple sample code AlternateViews: http://developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html
Although reading those documents, I could not create a oriented view that fits all my needs.
Supposing I would like to create an oriented loading view just with a background image, a status label and an activity indicator. I would create:
MyLoadingView*Base*ViewController - Which is a base class that keeps logic and it's IBOutlets by being the fileOwner of next XIBs.
MyLoadingView*Portrait*ViewController - Which inherits the MyLoadingViewBaseViewController above and have a personalized portrait view into a MyLoadingViewPortraitViewController.xib
MyLoadingView*Landscape*ViewController - Which also inherits the MyLoadingViewBaseViewController and have a personalized landscape view into a MyLoadingViewLandscapeViewController.xib.
What I have learned indicates that a portrait class must contain a property which keeps an instance of the landscape class.
@interface MyLoadingViewPortraitViewController : MyLoadingViewBaseViewController {
BOOL _isShowingLandscapeView;
MyLoadingViewLandscapeViewController *_landscapeViewController;
} @end
And when the device is rotated, the portrait view instance must present the landscape view instance over it has a modal view:
- (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;
}
}
That works, but let's talk about some Issues:
The default iPad rotation animation does not appear, the views swap suddenly resulting in an undesired experience to the user.
When trying to fix it I found into the AlternateViews sample code the lines bellow.- (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]; }
But the final rotation animation with it also does not appear like a default rotation of a view using autosizingMasks.
I am not comfortable about using a modal view presentation. Think about trying to rotate a view that is current defined as the detailed view controller into a UISplitViewController. If the detailed view implements this code, after a device rotation it would appear over the split view.
(Maybe adding as a subview can be a different solution. Did some of you tried it?)Why not use the UIViewController "Responding to View Rotation Events" methods instead of listening for UIDeviceOrientationDidChangeNotification. (When the landscape view is the frontmost view over the window, it will receive those rotate event methods and can resend to the portrait view which will continue managing it.)
Finally, I would appreciate for any answers, tips or sample codes for being creating a oriented view.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论