Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Animating Navigation Bar properties on page transitions

$
0
0

I am looking to add a transition to navigation bar text when navigating between views in a MasterDetail layout. Just a simple fade out / in of the navigation page text.

I have implemented the below custom renderer, which is getting me 99% of the way.

The problem I am having is that it appears when calling PushASync method, the navigation bar text is being updated before the point that SetupPageTransition is called in the Custom NavigationPageRenderer. By debugging I can also see this text is updated before the outgoing pages OnDisppearing method is called.

I can't seem to find any way to trigger the animation before the text is updated, so at the moment the Push transitions behave strangely as it jumps to the new title before fading out. In reality, this was the reason I decided to add this transition in the first place, as I found this behaviour jumpy.

Is there anyway i can trigger this animation before the text is updated?

public class CustomNavigationPageRenderer : NavigationPageRenderer
{
    private Android.Support.V7.Widget.Toolbar toolbar;

    private Android.Support.V7.Widget.AppCompatTextView _textView;

    public CustomNavigationPageRenderer(Context context)
        : base(context)
    {
    }

    public override void OnViewAdded(Android.Views.View child)
    {
        base.OnViewAdded(child);
        if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
        {
            toolbar                     = (Android.Support.V7.Widget.Toolbar)child;
            toolbar.Elevation           = 0;
            toolbar.ChildViewAdded      += Toolbar_ChildViewAdded;
        }
    }

    private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
    {
        var view                    = e.Child.GetType();

        if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
        {
            var textView            = (Android.Support.V7.Widget.AppCompatTextView)e.Child;
            var font                = Typeface.CreateFromAsset(this.Context.Assets, "gothambold.ttf");
            textView.Typeface       = font;
            toolbar.ChildViewAdded  -= Toolbar_ChildViewAdded;

            _textView = textView;


        }
    }



    protected override void SetupPageTransition(Android.Support.V4.App.FragmentTransaction transaction, bool isPush)
    {

        HandleTextFade();

        base.SetupPageTransition(transaction, isPush);
    }

    private void HandleTextFade()
    {

        if(_textView != null)
        {
            var colorAnim = ObjectAnimator.OfInt(_textView, "textColor", Android.Graphics.Color.White, Android.Graphics.Color.Transparent);
            colorAnim.SetEvaluator(new ArgbEvaluator());
            colorAnim.SetDuration(250);
            colorAnim.Start();

            colorAnim.AddListener(new OMNIAnimListener(_textView));
        }

    }

}

public class OMNIAnimListener : AnimatorListenerAdapter
{
    Android.Support.V7.Widget.AppCompatTextView TextView;

    public OMNIAnimListener(Android.Support.V7.Widget.AppCompatTextView textView)
        : base()
    {
        TextView = textView;
    }

    public override void OnAnimationEnd(Animator animation)
    {

        var colorAnim = ObjectAnimator.OfInt(TextView, "textColor", Android.Graphics.Color.Transparent, Android.Graphics.Color.White);
        colorAnim.SetEvaluator(new ArgbEvaluator());
        colorAnim.SetDuration(250);
        colorAnim.Start();

        base.OnAnimationEnd(animation);
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>