Search code examples
iosxamarin.iosxamarinmvvmcross

MvvmCross ViewModel transition from the left


I am developing an app for iOS using MvvmCross. On one of my Views I have some basic report data that is displayed in a tableview.

When the table row is touched a new view containing a detail report is displayed by making the call to ShowViewModel passing some parameters in a Dictionary. This works fine.

When the user swipes left or right the app needs to show the detail report for the next or previous item in the original list. I am doing this by updating some parameters and calling ShowViewModel again. The logic behind this is all working fine.

My problem; ShowViewModel animates the new view coming in from the right. This is perfect when the user has swiped left. However when swiping right it seems counter intuitive. How can I make ShowViewModel animate or transition in from the left side?


Solution

  • This is the solution I was able to come up with following the helpful pointers in the answer from Andrei N. In the end I opted for a TransitionFlipFromRight and TransitionFlipFromLeft when scrolling between detail reports. Hopefully it is useful to somebody else.

    I already had a presenter class that was inherited from MvxModalSupportTouchViewPresenter

    public class BedfordViewPresenter : MvxModalSupportTouchViewPresenter
    

    Within this class I added a property of MvxPresentationHint.

    private MvxPresentationHint _presentationHint;
    

    In the override of method ChangePresentation the above property is used to store the passed in parameter

        public override void ChangePresentation (MvxPresentationHint hint)
        {
            _presentationHint = hint;
            base.ChangePresentation (hint);
        }
    

    Two new MvxPresentationHint class were declared (see later)

    In the presenter class the Show method was overridden

        public override void Show(IMvxTouchView view)
        {
            if (_presentationHint is FlipFromRightPresentationHint) {
                var viewController = view as UIViewController;
                MasterNavigationController.PushControllerWithTransition (viewController, UIViewAnimationOptions.TransitionFlipFromRight);
    
            }
            else
                if (_presentationHint is FlipFromLeftPresentationHint)  {
                    var viewController = view as UIViewController;
                    MasterNavigationController.PushControllerWithTransition (viewController, UIViewAnimationOptions.TransitionFlipFromLeft);
                }
                else {
                    base.Show (view);
                }
    
            _presentationHint = null;
        }
    

    A new class that provides extensions to a UINavigationController was created with the method PushControllerWithTransition

    public static class UINavigationControllerExtensions
    {
        public static void PushControllerWithTransition(this UINavigationController 
            target, UIViewController controllerToPush, 
            UIViewAnimationOptions transition)
        {
            UIView.Transition(target.View, 0.75d, transition, delegate() {
                target.PushViewController(controllerToPush, false);
            }, null);
        }
    }
    

    All that needs to be defined now are the two new MvxPresentationHint class derivations. These belong in your Core class library project rather than the iOS application project.

    public class FlipFromLeftPresentationHint : MvxPresentationHint
    {
        public FlipFromLeftPresentationHint ()
        {
        }
    }
    

    and

    public class FlipFromRightPresentationHint: MvxPresentationHint
    {
        public FlipFromRightPresentationHint ()
        {
        }
    }
    

    I hope this is a help to someone else trying to do something similar