程序化视图正确旋转

时间:2012-06-26 22:24:33

标签: iphone ios uiview orientation device-orientation

我一直在以编程方式完成我的所有用户界面,并在Interface Builder中执行了一些细微的改动..但是99%的UI都是在代码中完成的,因为我觉得有一定的灵活性来自这样做。

但是我现在遇到了处理设备旋转的问题,因为我有几个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

我想知道是否有比通过所有这些麻烦更简单的方法!我仍然相当缺乏经验,所以这是我最好的尝试..我希望有人知道一个更简单的方法,而不是做所有这些腿部工作,以便将视图添加到其他视图作为子视图正确调整方向

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

1 个答案:

答案 0 :(得分:1)

请参阅autoresizingMask documentation。为您提供与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不够用(例如,当您相互移动对象以真正微调纵向与横向方向时),我建议您在{{ {3}}适用于iOS5,(或iOS4或更早版本中的viewWillLayoutSubviews)。这样您就不需要自己动画更改,动画将与屏幕旋转动画的其余部分一起完成。