Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

simulate text typing on a label

dalton5dalton5 Member ✭✭
edited March 10 in Xamarin.Forms

Hi,

I try to create an animation of typing but the final result is very bad and not smooth.

How can I improve my code to get it smooth.

Simulate key typing? Another way exists?

Thanks

My code is as below:

public class AnimatedLabel : Label
{
    public AnimatedLabel() : base()
    {

    }

    private static BindableProperty animatedTextProperty = BindableProperty.Create(
                                                       propertyName: "AnimatedText",
                                                       returnType: typeof(string),
                                                       declaringType: typeof(AnimatedLabel),
                                                       defaultValue: "",
                                                       defaultBindingMode: BindingMode.TwoWay,
                                                       propertyChanged: titleTextPropertyChanged);



    public string AnimatedText
{
        get => (string)GetValue(animatedTextProperty);
        set => SetValue(animatedTextProperty, value);
    }

    private static void titleTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (AnimatedLabel)bindable;
        string val = newValue.ToString();


        //for(int i=0; i<val.Length;i++)
        //{
        int i = 0;

            Device.StartTimer(new TimeSpan(0,0,0,0,10), () =>
            {
                Debug.WriteLine("Value: " + control.Text + val[i]);
                Device.BeginInvokeOnMainThread(() =>
                {
                    control.Text = control.Text + val[i];
                    i++;
                });

                if (i == val.Length - 1)
                    return false;

                return true;
            });

        //}
   }   

}

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    but the final result is very bad and not smooth.

    Based on my test, your code works properly.
    What exactly do you want it to look like?

Sign In or Register to comment.