如何在 Xamarin.Ios (C#,Xaml) 中使我的动画更加平滑/流畅

问题描述 投票:0回答:1

我尝试为无法完全适合框视图的标签制作自动滚动,因此我使用两个字符列表,一个可见,另一个不可见,并将字符从一个切换到另一个,这一点也不流畅导致我的标签在消失的字符大小的函数中向左跳转,所以我使用 Translation X 在消失的字符大小的函数中向右跳转,并使用 TranslationTo 使跳转变慢并进行控制,事实是我无法准确知道字符的像素大小,所以我现在自己输入值,您是否知道我如何知道像素大小,或者您是否有另一个想法让我的标签自动滚动。

我已经尝试过宽度,结果总是-1,我已经尝试调用 TextRenderer.MesureText 但无法使用它,我已经尝试过 Graphics.MeasureString 但此平台不支持 System.Drawing.Common,也许我没有没有正确使用它们,如果您能帮助我,我将非常感激。

private async void StartAnimation3()
    {
        List<char> firstQueue = new List<char>();         //to stock what I d'ont display
        List<char> secondQueue = new List<char>();          //visible one 

        int initialX = (int)metroLabel.TranslationX; 
        int sizeStringVisble = 60;                                 //width of label I can display I suppose it's close to 60 chars
        firstQueue.AddRange(new char[] { ' ', '/', ' ' }); 
        string texteComplet = "Température bureau 20°C - 15000 lumens - Volets a 80%";      //my text

        foreach (char s in texteComplet)     //I fill my lists
        {
            if (secondQueue.Count < sizeStringVisble)
            { 
                secondQueue.Add(s);
            }
            else
            {
                firstQueue.Add(s);
            }  
        }
        
        if (secondQueue.Count < sizeStringVisble)   //fill them with space to reach the width of my label 
        {
            int nbEspace = sizeStringVisble - (secondQueue.Count);
            for (int i= 0; i < nbEspace; i++)
            {
                secondQueue.Add(' ');
            }
        }

        while (true)
        {
            if (secondQueue.Count < sizeStringVisble)    //I switch my chars
            {

                if (secondQueue.Count != 0)
                {
                    char s = secondQueue[0];
                    secondQueue.RemoveAt(0);
                    firstQueue.Add(s);
                }

                if (firstQueue.Count != 0)
                {
                    char s = firstQueue[0];
                    firstQueue.RemoveAt(0);
                    secondQueue.Add(s);
                }

            }
            else
            {
                char s = secondQueue[0];
                secondQueue.RemoveAt(0);
                firstQueue.Add(s);

            }


            if (firstQueue.Last() == ' ' || firstQueue.Last() == '/' || firstQueue.Last() == '°') 
            {
                metroLabel.TranslationX += 5;    //I suppose that this type of char are 5 pixels 
                await metroLabel.TranslateTo(initialX, metroLabel.TranslationY, 80);
                
            }
            else if (char.IsUpper(firstQueue.Last()) || firstQueue.Last() == '%')
            {
                metroLabel.TranslationX += 7;     //I suppose that this type of char are 7 pixels
                await metroLabel.TranslateTo(initialX, metroLabel.TranslationY, 175);
            }
            else
            {
                metroLabel.TranslationX += 6;    //I suppose that this type of char are 6 pixels
                await metroLabel.TranslateTo(initialX, metroLabel.TranslationY, 150);
            }

            metroLabel.Text = String.Join("", secondQueue.ToArray());
        }
c# xamarin xamarin.ios width translation
1个回答
0
投票

您可以尝试使用 ScrollView 来包裹标签,

<AbsoluteLayout>
    <BoxView AbsoluteLayout.LayoutBounds="0, 10, 400, 50" />
    <ScrollView x:Name="myscroll" Orientation="Horizontal" HorizontalScrollBarVisibility="Never"
                WidthRequest="400"
                AbsoluteLayout.LayoutBounds="0, 20, AutoSize, AutoSize">
        <Label x:Name="label" Text="long text here......"
           FontSize="24"
           />
    </ScrollView>
</AbsoluteLayout>

因为滚动视图可以通过编程方式滚动,你可以尝试使用

await myscroll.ScrollToAsync(label, ScrollToPosition.End, true);

控制ScrollView的滚动。

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.