Search code examples
iosanimationxamarinmvvmcross

MvvmCross v5 animation between two root presentations


I have two iOS views marked by MvxRootPresentation attribute: LoginView without wrapping into navigation controller and MainView with wrapping into navigation controller.

When I call ShowViewModel<MainViewModel>() there is no animation between these two views. All subsequent views are animated as usual (within NavigationController).

How can I set animation for this transition?


Solution

  • Ok, I've did it myself :) I had to add my custom presentation attribute and custom presenter:

    public class AnimatedRootPresentationAttribute : MvxRootPresentationAttribute
    {
    }
    
    public class MyPresenter : MvxIosViewPresenter
    {
        public MyPresenter(IUIApplicationDelegate appDelegate, UIWindow window)
            : base(appDelegate, window)
        {
        }
    
        protected override void RegisterAttributeTypes()
        {
            base.RegisterAttributeTypes();
    
            _attributeTypesToShowMethodDictionary.Add(typeof(AnimatedRootPresentationAttribute),
                (viewController, attribute, request) => ShowAnimatedRootViewController(
                    viewController, (AnimatedRootPresentationAttribute)attribute, request));
        }
    
        private void ShowAnimatedRootViewController(
            UIViewController viewController,
            AnimatedRootPresentationAttribute attribute,
            MvxViewModelRequest request)
        {
            ShowRootViewController(viewController, attribute, request);
            AddAnimation();
        }
    
        private void AddAnimation()
        {
            var transition = new CATransition
            {
                Duration = 0.2,
                Type = CAAnimation.TransitionMoveIn,
                Subtype = CAAnimation.TransitionFromTop
            };
    
            _window.RootViewController.View.Layer.AddAnimation(transition, null);
        }
    }