首页 > 解决方案 > 有没有办法从两侧以编程方式为按钮的宽度设置动画

问题描述

我希望能够从两侧动态调整按钮的大小,现在我能够从一侧从右到左做到这一点。

代码 ->

double Height = Button1.Height;
bool x = await Button1.LayoutTo(new Rectangle(new Point(Button1.X, Button1.Y), new Size(150, Height)), 1200,Easing.Linear);

按钮 ->

<StackLayout VerticalOptions="Center">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button x:Name="Button1" Grid.Row="0" Clicked="Button1_Clicked" BorderRadius="20" HorizontalOptions="CenterAndExpand" WidthRequest="1500" />
            <!--<ActivityIndicator Grid.Row="1" IsRunning="true" ></ActivityIndicator>-->
        </Grid>
    </StackLayout>

(动画前)->

(动画后)->

![](https://i.imgur.com/yFGbAq0.png?1)

(我想要)->

![](https://i.imgur.com/eLfO6Rt.png?1)

标签: c#xamarin.forms

解决方案


如果您只打算调整大小,请使用ScaleXTo设置AnchorX为 0.5(默认)。

在 XAMl 中设置 AnchorX:

<Button
    x:Name="Button1"
    Grid.Row="0"
    Clicked="Button1_Clicked"
    BorderRadius="20"
    AnchorX="0.5"
    HorizontalOptions="CenterAndExpand"
    WidthRequest="1500" />

在 cs 中使用ScaleXTo

void Button1_Clicked(System.Object sender, System.EventArgs e)
{
    if (Button1.ScaleX < 1)
    {
        Button1.ScaleXTo(1);
    }
    else
    {
        Button1.ScaleXTo(0.5);
    }
}

如果需要更多信息,请试一试并发表评论。希望能帮助到你。


推荐阅读