0

我想獲得新的IOS 5容器viewControllers正常工作,但我有一個問題在他們之間動畫。在同一屏幕上的子視圖控制器之間的IOS過渡

我有一個「rootViewController」。在這個控制器中,我添加了2個子視圖控制器。這個功能幾乎就像一個splitViewController。在左側,我有一個處理導航的VC,在右側,我有一個顯示特定內容的VC。

我正在嘗試在右側的VC和將替換它的新VC之間進行動畫製作。

這是我的動畫代碼:

public void Animate(UIViewController toController) { 
    AddChildViewController(toController); 
    activeRightController.WillMoveToParentViewController(null); 
    toController.View.Frame = activeRightController.View.Frame; 
    Transition(activeRightController, toController, 1.0, UIViewAnimationOptions.TransitionCurlUp,() => {}, 
     (finished) => { 
     activeRightController.RemoveFromParentViewController(); 
     toController.DidMoveToParentViewController(this); 
     activeRightController = toController; 
    }); 
    activeRightController = toController; 
} 

幾乎所有的功能,它轉變到我使用CurlUp過渡新觀點,但是轉型本身變爲在整個屏幕上......不只是單一的認爲我想要過渡。它「蜷縮」父母的觀點,而不是孩子。但它只會替換它下面的子視圖。我希望這是有道理的。

回答

6

我創建了一個新的UIViewController類,它是右側控制器的精簡容器並處理交換動畫。請參閱下面的示例。

using System; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using System.Drawing; 

namespace delete20120425 
{ 
    [Register ("AppDelegate")] 
    public partial class AppDelegate : UIApplicationDelegate 
    { 
     UIWindow window; 
     MainViewController mvc; 

     public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
     { 
      window = new UIWindow (UIScreen.MainScreen.Bounds); 

      mvc = new MainViewController(); 

      window.RootViewController = mvc; 
      window.MakeKeyAndVisible(); 

      return true; 
     } 
    } 

    public class MainViewController : UIViewController 
    { 
     SubViewController vc1; 
     ContainerViewController vc2; 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      vc1 = new SubViewController (UIColor.Blue); 
      this.AddChildViewController (vc1); 
      this.View.AddSubview (vc1.View); 

      UIButton btn = UIButton.FromType (UIButtonType.RoundedRect); 
      btn.Frame = new RectangleF (20, 20, 80, 25); 
      btn.SetTitle ("Click", UIControlState.Normal); 
      vc1.View.AddSubview (btn); 

      SubViewController tmpvc = new SubViewController (UIColor.Yellow); 
      vc2 = new ContainerViewController (tmpvc); 

      this.AddChildViewController (vc2); 
      this.View.AddSubview (vc2.View); 

      btn.TouchUpInside += HandleBtnTouchUpInside; 
     } 

     void HandleBtnTouchUpInside (object sender, EventArgs e) 
     { 
      SubViewController toController = new SubViewController (UIColor.Green); 
      vc2.SwapViewController (toController); 
     } 

     public override void ViewWillLayoutSubviews() 
     { 
      base.ViewDidLayoutSubviews(); 

      RectangleF lRect = this.View.Bounds; 
      RectangleF rRect = lRect; 

      lRect.X = lRect.Y = lRect.Y = 0; 

      lRect.Width = .3f * lRect.Width; 
      rRect.X = lRect.Width + 1; 
      rRect.Width = (.7f * rRect.Width)-1; 

      this.View.Subviews[0].Frame = lRect; 
      this.View.Subviews[1].Frame = rRect; 
     } 

     public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) 
     { 
      return true; 
     } 
    } 

    public class ContainerViewController : UIViewController 
    { 
     UIViewController _ctrl; 
     public ContainerViewController (UIViewController ctrl) 
     { 
      _ctrl = ctrl; 
     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      AddChildViewController (_ctrl); 
      View.AddSubview (_ctrl.View); 
     } 

     public void SwapViewController (UIViewController toController) 
     { 
      UIViewController activeRightController = _ctrl; 

      AddChildViewController(toController); 
      activeRightController.WillMoveToParentViewController(null); 
      toController.View.Frame = activeRightController.View.Frame; 
      Transition(activeRightController, toController, 1.0, UIViewAnimationOptions.TransitionCurlUp,() => {}, 
       (finished) => { 
       activeRightController.RemoveFromParentViewController(); 
       toController.DidMoveToParentViewController(this); 
       activeRightController = toController; 
      }); 
      activeRightController = toController; 
     } 

     public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) 
     { 
      return true; 
     } 

     public override void ViewWillLayoutSubviews() 
     { 
      base.ViewWillLayoutSubviews(); 
      RectangleF tmp = this.View.Frame; 
      tmp.X = tmp.Y = 0; 
      _ctrl.View.Frame = tmp; 
     } 
    } 

    public class SubViewController : UIViewController 
    { 
     UIColor _back; 
     public SubViewController (UIColor back) 
     { 
      _back = back; 
     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
      this.View.BackgroundColor = _back; 
     } 

     public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) 
     { 
      return true; 
     } 
    } 
} 
+0

我不明白集裝箱VEW控制器,直到這個例子。非常感謝。這工作完美。 – 2012-04-26 21:57:57

+0

如果可能的話,我會投票兩次 – Omtara 2012-10-17 16:56:47

+0

你會如何回到以前的觀點? – PLOW 2016-09-20 14:47:26

相關問題