简体   繁体   English

在同一屏幕上的子viewController之间的iOS转换

[英]IOS Transition between child viewControllers on same screen

I am trying to get the new IOS 5 Container viewControllers working properly but I am having an issue animating between them. 我正在尝试使新的IOS 5容器viewControllers正常工作,但是它们之间存在动画问题。

I have a "rootViewController". 我有一个“ rootViewController”。 In this controller, I have added 2 child view controllers. 在此控制器中,我添加了2个子视图控制器。 This functions almost like a splitViewController. 该功能几乎类似于splitViewController。 On the left I have a VC that handles navigation, and on the right I have a VC that shows specific content. 左侧有一个用于处理导航的VC,而右侧有一个用于显示特定内容的VC。

I am trying to animate between the VC on the right, and a new VC which will replace it. 我正在尝试在右侧的VC和将替换它的新VC之间创建动画。

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. 几乎所有东西都可以使用,它使用CurlUp过渡过渡到我的新视图,但是过渡本身遍及整个屏幕...而不仅仅是我要过渡的单个视图。 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. 我希望这是有道理的。

I created a new UIViewController class that is a thin container for the right side controllers and handles the swap animation. 我创建了一个新的UIViewController类,该类是用于右侧控制器的瘦容器并处理交换动画。 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS中的子ViewController是否与Android中的Fragments相同? - Are Child ViewControllers in iOS same as Fragments in Android? ViewControllers之间的转换不起作用 - transition between ViewControllers not working ViewController之间的过渡不起作用 - Transition between ViewControllers is not working 如何使用 iOS swift hero 框架在两个视图控制器之间进行转换? - How to use iOS swift hero framework for transition between two viewcontrollers? ViewController之间的滞后自定义过渡 - Laggy Custom Transition between ViewControllers iOS:viewControllers之间的导航 - iOS: navigation between viewControllers iOS策略使视图控制器之间的按钮保持相同大小 - iOS strategy to keep buttons the same size between viewcontrollers (iOS)如何在两个ViewController之间的过渡中为UI元素(例如标签和图像)的过渡设置动画? - (iOS) How can i animate the traslation of UI elements, like labels and images in the transition between two viewcontrollers? 由rootView控制器呈现的两个viewController之间的过渡 - Transition between two viewControllers presented by rootView controller ViewControllers 之间的转换没有为正确的 VC 设置动画 - transition between ViewControllers not animating correct VC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM