2012-06-26 33 views
0

我一直在Interface Builder正在执行轻微的改变相当多的做我的所有用户界面的编程..但所有的UI的99%的代码做独家,因为我觉得有一定的通过这种方式获得的灵活性。程序化UIViews旋转正确

但是我现在有处理设备的旋转问题,因为我已经被添加为子视图几个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 

我就行云想知道是否有比通过所有这些麻烦更简单的方法!我仍然相当缺乏经验所以这是我最好的尝试..我希望有人在那里知道一个更简单的方法比过去的观点被添加到其他意见,子视图调整方向正确

不必做这一切跑腿的活儿

任何帮助将不胜感激!我绝望的笑:)

+0

您应该在这里使用** switch'**语句。 – WrightsCS

+0

hrmm ..我发现switch语句对于你在同一个变量上有几个选项或者你有什么的情况是有好处的。但是当我有一些变量的混合时,我需要检查一下,我发现很难判断哪个switch会很好..你认为我可以用交换机替换所有的if语句吗? – HurkNburkS

+0

为什么不在第一次创建帧时设置自动调整遮罩。这样,当你改变方向时它会自动调整大小? – Rob

回答

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是不够的(例如,当你移动相对于物体彼此真正微调纵向与横向),我建议你在iOS5的viewWillLayoutSubviews中执行此布局过程(或iOS4或更早版本中的willAnimateRotationToInterfaceOrientation)。这样你就不需要自己动画改变动画,并且动画将与屏幕旋转动画的其余部分一起完成。

+0

真棒!谢谢一堆! – HurkNburkS

+1

今天** mySubview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; **拯救了我的生命;)再次欢呼伙计..代码看起来好多了 – HurkNburkS