0

我一直在以编程方式完成几乎所有的用户界面,并在界面生成器中执行了一些细微的更改。但是所有 UI 的 99% 都是在代码中完成的,因为我觉得这样做可以获得一定的灵活性方式。

但是我现在在处理设备的旋转时遇到了问题,因为我有几个 UIViews 被添加为子视图我面临一个旋转问题,因为这是我通常声明视图的方式

htmlTest.webViewTest.frame = CGRectMake(4.0, 4.0, 312.0, 363.0);

并且由于设备旋转时这个固定的 CGRectMake 视图保持相同的大小并且正确地适合视图的方向。

所以我研究了一个在我看来很糟糕的解决方案。我在其中制作了几个视图,用户可以从中选择选项,然后我将它们动画出来。但是他们需要能够处理任何一个中的加载纵向或横向,然后如果在加载它们时,它们需要能够处理从任一方向到另一个方向的旋转。

这就是我所做的观点之一。

#pragma createAwesomeJumpBar
- (void)jumpBarButtonPosition:(int)changeView
{
    // ChangeView is used to check if the this method is being called from a device rotation or from a button press (0, being rotation and 1, being tabbarButton touch

    // if tabbar selected
    if (changeView == 1) {
        if  ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
        {
            if (![jumpBarContainerPortrait superview]) {
                // load portrait view
                jumpBarContainerPortrait = [[UIView alloc] initWithFrame:CGRectMake(0.0, 480.0, 320, (jumpBarHeightPortrait + 49.0))];
                jumpBarContainerPortrait.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

                // add jumpbar container to view
                [self.view insertSubview:jumpBarContainerPortrait belowSubview:actionTabBar];

                [UIView animateWithDuration:0.6
                                      delay:0.0f
                                    options:UIViewAnimationCurveEaseIn 
                                 animations:^{

                                     jumpBarContainerPortrait.frame = CGRectMake(0.0, (367 - jumpBarHeightPortrait), 320.0, (jumpBarHeightPortrait + 49.0)); // display jumpBar


                                 } completion:^(BOOL finished) {
                                     if (finished) {
                                         NSLog(@"YAY!");
                                     }
                                 }];
            }
            else if ([jumpBarContainerPortrait superview]) {
                //unload portrait view
                [UIView animateWithDuration:0.6
                                      delay:0.0f
                                    options:UIViewAnimationCurveEaseIn 
                                 animations:^{

                                     jumpBarContainerPortrait.frame = CGRectMake(0.0, 480.0, 320.0, (jumpBarHeightPortrait + 49.0)); // display jumpBar

                                     // remove selected tabButton highlight
                                     [actionTabBar setSelectedItem:nil];


                                 } completion:^(BOOL finished) {
                                     if (finished) {

                                         // remove subView for superView
                                         [jumpBarContainerPortrait removeFromSuperview];

                                     }
                                 }];

            }

        }
        else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
        {
            if (![jumpBarContainerLandscape superview]) {
                // load landscape view
                jumpBarContainerLandscape = [[UIView alloc] initWithFrame:CGRectMake(0.0, 320, 480.0, (jumpBarHeightLandscape + 49.0))];
                jumpBarContainerLandscape.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

                // add jumpbar container to view
                [self.view insertSubview:jumpBarContainerLandscape belowSubview:actionTabBar];

                [UIView animateWithDuration:0.6
                                      delay:0.0f
                                    options:UIViewAnimationCurveEaseIn 
                                 animations:^{

                                     jumpBarContainerLandscape.frame = CGRectMake(0.0, (207 - jumpBarHeightLandscape), 480.0,  (jumpBarHeightLandscape + 49.0)); // display jumpBar


                                 } completion:^(BOOL finished) {
                                     if (finished) {
                                         NSLog(@"YAY!");
                                     }
                                 }];
            }
            else if ([jumpBarContainerLandscape superview]) {
                // remove landscape view
                [UIView animateWithDuration:0.6
                                      delay:0.0f
                                    options:UIViewAnimationCurveEaseIn 
                                 animations:^{

                                     jumpBarContainerLandscape.frame = CGRectMake(0.0, 320, 480.0, (jumpBarHeightLandscape + 49.0)); // display jumpBar

                                     [actionTabBar setSelectedItem:nil];

                                 } completion:^(BOOL finished) {
                                     if (finished) {

                                         // remove subView for superView
                                         [jumpBarContainerLandscape removeFromSuperview];
                                     }
                                 }];
            }

        }
    }
    // if device rotated selected
    else if (changeView == 0) {
        if  ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
        {
            if([jumpBarContainerLandscape superview])
            {
            // Device is changing from landscape to protrait change views to fit
            // load landscape view
            jumpBarContainerPortrait = [[UIView alloc] initWithFrame:CGRectMake(0.0, (367 - jumpBarHeightPortrait), 320.0, (jumpBarHeightPortrait + 49.0))];
            jumpBarContainerPortrait.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
            jumpBarContainerPortrait.alpha = 1.0;

            // add jumpbar container to view

                [UIView transitionFromView:jumpBarContainerLandscape
                                    toView:jumpBarContainerPortrait
                                  duration:animationSpeed
                                   options:UIViewAnimationOptionTransitionCrossDissolve 
                                completion:NULL];

                [self.view insertSubview:jumpBarContainerPortrait belowSubview:actionTabBar];


            }
        }
        else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
        {
            if ([jumpBarContainerPortrait superview])
            {
            // Device is changing from portrait to landscape change views to fit
            // load landscape view
            jumpBarContainerLandscape = [[UIView alloc] initWithFrame:CGRectMake(0.0, (207 - jumpBarHeightLandscape), 480.0,  (jumpBarHeightLandscape + 49.0))];
            jumpBarContainerLandscape.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
            jumpBarContainerLandscape.alpha = 1.0;

            // add jumpbar container to view

                [UIView transitionFromView:jumpBarContainerPortrait
                                    toView:jumpBarContainerLandscape
                                  duration:animationSpeed
                                   options:UIViewAnimationOptionTransitionCrossDissolve
                                completion:NULL];

                [self.view insertSubview:jumpBarContainerLandscape belowSubview:actionTabBar];



            }

        }
    }
}

在这个例子中,我有两个视图横向和纵向,显然每个名称都是针对它们各自的方向.. 上面的逻辑遵循这个

if tabbarselected 

if !view visible
if device orientation portrait 
animate in portrait view.
if device orientation landscape
animate in landscape view

if view visible
if device orientation portrait
animate out portrait view
clear tabbar
if device orientation landscape
animate out landscape view
clear tabbar

if !tabbarselected //meaning listener has identified orientation of device has changed 

if device orientation portrait
unload portrait 
load landscape

if device orientation landscape
unload landscape
load portrait

我想知道是否有比经历所有这些麻烦更简单的方法!我仍然相当缺乏经验,所以这是我最好的尝试.. 我希望那里的人知道一种更简单的方法,而不是必须做所有这些腿部工作才能将视图添加到其他视图中,因为子视图可​​以正确调整方向

任何帮助将不胜感激!我很绝望哈哈:)

4

1 回答 1

1

请参阅autoresizingMask文档。为您提供与 Interface Builder 中相同的弹簧和支柱控制。例如:

CGRect frame = CGRectMake(margin, margin, self.view.frame.size.width - margin * 2, self.view.frame.size.height - margin * 2);
UIView *mySubview = [[UIView alloc] initWithFrame:frame];
[self.view mySubview];
mySubview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

此外,如果您认为 autoresizingMask 还不够(例如,当您相对于彼此移动对象以真正微调纵向与横向方向时),我建议您在viewWillLayoutSubviewsiOS5 中执行此布局过程,(或willAnimateRotationToInterfaceOrientation在 iOS4 或更早版本中)。这样您就不需要自己为更改设置动画,并且动画将与屏幕旋转动画的其余部分一起完成。

于 2012-06-26T22:52:12.197 回答