Переход IOS между дочерними контроллерами просмотра на одном экране

Я пытаюсь заставить новые контейнерные контроллеры IOS 5 работать правильно, но у меня возникла проблема с анимацией между ними.

У меня есть «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, однако сам переход проходит через ВЕСЬ экран... а не только один вид, который я хочу перевести. Это «скручивание» родительского представления, а не дочернего. Однако он заменяет только дочерний вид под ним. Я надеюсь это имеет смысл.


person Chris Kooken    schedule 26.04.2012    source источник


Ответы (1)


Я создал новый класс 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;
        }
    }
}
person holmes    schedule 26.04.2012
comment
Я не совсем понимал контроллеры Container Vew до этого примера. Большое спасибо. Это сработало отлично. - person Chris Kooken; 27.04.2012
comment
Как бы вы вернулись к предыдущему виду? - person PLOW; 20.09.2016