2012-06-26 76 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