IOS在同一屏幕上的子viewControllers之间转换

时间:2012-04-26 00:09:38

标签: ios uiviewcontroller xamarin.ios uiviewanimationtransition

我正在尝试让新的IOS 5 Container viewControllers正常工作,但我在它们之间存在动画问题。

我有一个“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过渡转换到我的新视图,但过渡本身跨越整个屏幕...而不仅仅是我想要转换的单个视图。它的“卷曲”是父母的观点,而不是孩子。但它只会替换它下面的子视图。我希望这是有道理的。

1 个答案:

答案 0 :(得分: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;
        }
    }
}