0

I am trying to get the new IOS 5 Container viewControllers working properly but I am having an issue animating between them.

I have a "rootViewController". In this controller, I have added 2 child view controllers. This functions almost like a splitViewController. On the left I have a VC that handles navigation, and on the right I have a VC that shows specific content.

I am trying to animate between the VC on the right, and a new VC which will replace it.

This is my code for the animation:

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;
}

Almost everything works, it transitions to my new view using the CurlUp transition, however the transition itself goes across the WHOLE screen...not just the single view that I want to transition. Its "curling Up" the parent view, and not the child. It does however replace only the child view underneath it. I hope this makes sense.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123

1 Answers1

6

I created a new UIViewController class that is a thin container for the right side controllers and handles the swap animation. See the sample below.

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;
        }
    }
}
holmes
  • 1,341
  • 9
  • 9