WPF动画:如何使其滑入? [英] WPF Animation : How do i make it slide in?

查看:82
本文介绍了WPF动画:如何使其滑入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚接触动画,我想做一个滑出"动画.动画,我设法做到了. 但是现在我希望它可以通过单击同一按钮来滑入. 因此,就像我单击它一样,它滑出,然后再次单击时希望它滑回.

没有任何代码,因此只需通过xaml

这是XAML

<Grid>
    <Grid.Resources>
        <Storyboard x:Key="TransformImage">
            <DoubleAnimation
                Storyboard.TargetName="MovingImage"
                Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)"
                By="130" Duration="0:0:0.3">
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="TransformButton">
            <DoubleAnimation
                Storyboard.TargetName="btnChange"
                Storyboard.TargetProperty="(Button.RenderTransform).(TranslateTransform.X)"
                By="130" Duration="0:0:0.3">
            </DoubleAnimation>
        </Storyboard>
    </Grid.Resources>


    <Grid.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btnChange">
            <BeginStoryboard Storyboard="{StaticResource TransformImage}"/>
            <BeginStoryboard Storyboard="{StaticResource TransformButton}"/>
        </EventTrigger>
    </Grid.Triggers>

    <StackPanel Orientation="Horizontal" Margin="0">
        <Image x:Name="MovingImage" Source="logo.png"
               MaxWidth="120">
            <Image.RenderTransform>
                <TranslateTransform />
            </Image.RenderTransform>
        </Image>
    </StackPanel>

    <StackPanel
        Panel.ZIndex="1"
        Height="450" 
          Width="120"
          HorizontalAlignment="Left"
          Background="Black"></StackPanel>

    <Button Margin="130,0,0,0" Height="40" Width="120"
            Content="Show Image" x:Name="btnChange"
            HorizontalAlignment="Left" >
        <Button.RenderTransform>
            <TranslateTransform />
        </Button.RenderTransform>
    </Button>
</Grid>

解决方案

单击按钮并基于其运行适当的故事板时,您需要存储幻灯片动画的当前状态.

例如,您可以使用具有IsChecked属性(或视图模型中的属性)的ToggleButton.

为避免重复的触发器/样式,我将Image和ToggleButton放在了同一StackPanel中.

<Grid>
    <Grid.Resources>

        <system:Double x:Key="SlideOffSet">130</system:Double>

        <Storyboard x:Key="SlideRight">
            <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
                             From="0" To="{StaticResource SlideOffSet}"
                             Duration="0:0:0.3" />
         </Storyboard>

         <Storyboard x:Key="SlideLeft">
             <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
                              From="{StaticResource SlideOffSet}" To="0" 
                              Duration="0:0:0.3" />
         </Storyboard>

    </Grid.Resources>

    <StackPanel Orientation="Horizontal" Margin="0">
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=SlideState}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource SlideRight}" />
                        </DataTrigger.EnterActions>
                        <DataTrigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource SlideLeft}" />
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>

        <StackPanel.RenderTransform>
            <TranslateTransform />
        </StackPanel.RenderTransform>

        <Image Source="logo.png" MaxWidth="120" />
        <ToggleButton x:Name="SlideState" Margin="10,0,0,0" Height="40" Width="120" Content="Show Image" />

    </StackPanel>

    <StackPanel
        Panel.ZIndex="1"
        Height="450" 
        Width="120"
        HorizontalAlignment="Left"
        Background="Black"></StackPanel>
</Grid>

此外,我不建议使用DoubleAnimationBy属性:
如果动画仍在运行,然后再次单击按钮,则第二个动画将在错误的位置开始(它将使用第一个动画的X值,但尚未完成)-多次快速单击按钮,您将看到问题.

改为使用FromTo属性.

So I just got into animations and I wanted to do a "Slide out" animation and I managed to do so just fine. But now I want it to slide in with the click on the same button. So like I click it and it slides out and then I want it to slide back in when I click it again.

WITHOUT any code behind, so just through xaml

Here is the XAML

<Grid>
    <Grid.Resources>
        <Storyboard x:Key="TransformImage">
            <DoubleAnimation
                Storyboard.TargetName="MovingImage"
                Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)"
                By="130" Duration="0:0:0.3">
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="TransformButton">
            <DoubleAnimation
                Storyboard.TargetName="btnChange"
                Storyboard.TargetProperty="(Button.RenderTransform).(TranslateTransform.X)"
                By="130" Duration="0:0:0.3">
            </DoubleAnimation>
        </Storyboard>
    </Grid.Resources>


    <Grid.Triggers>
        <EventTrigger RoutedEvent="Button.Click" SourceName="btnChange">
            <BeginStoryboard Storyboard="{StaticResource TransformImage}"/>
            <BeginStoryboard Storyboard="{StaticResource TransformButton}"/>
        </EventTrigger>
    </Grid.Triggers>

    <StackPanel Orientation="Horizontal" Margin="0">
        <Image x:Name="MovingImage" Source="logo.png"
               MaxWidth="120">
            <Image.RenderTransform>
                <TranslateTransform />
            </Image.RenderTransform>
        </Image>
    </StackPanel>

    <StackPanel
        Panel.ZIndex="1"
        Height="450" 
          Width="120"
          HorizontalAlignment="Left"
          Background="Black"></StackPanel>

    <Button Margin="130,0,0,0" Height="40" Width="120"
            Content="Show Image" x:Name="btnChange"
            HorizontalAlignment="Left" >
        <Button.RenderTransform>
            <TranslateTransform />
        </Button.RenderTransform>
    </Button>
</Grid>

解决方案

You need to store current state of slide animation when clicking on button and based on it run appropriate story board.

For example, you could use ToggleButton that has IsChecked property (or property in view-model).

To avoid duplicate triggers/styles I placed Image and ToggleButton in the same StackPanel.

<Grid>
    <Grid.Resources>

        <system:Double x:Key="SlideOffSet">130</system:Double>

        <Storyboard x:Key="SlideRight">
            <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
                             From="0" To="{StaticResource SlideOffSet}"
                             Duration="0:0:0.3" />
         </Storyboard>

         <Storyboard x:Key="SlideLeft">
             <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
                              From="{StaticResource SlideOffSet}" To="0" 
                              Duration="0:0:0.3" />
         </Storyboard>

    </Grid.Resources>

    <StackPanel Orientation="Horizontal" Margin="0">
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=SlideState}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource SlideRight}" />
                        </DataTrigger.EnterActions>
                        <DataTrigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource SlideLeft}" />
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>

        <StackPanel.RenderTransform>
            <TranslateTransform />
        </StackPanel.RenderTransform>

        <Image Source="logo.png" MaxWidth="120" />
        <ToggleButton x:Name="SlideState" Margin="10,0,0,0" Height="40" Width="120" Content="Show Image" />

    </StackPanel>

    <StackPanel
        Panel.ZIndex="1"
        Height="450" 
        Width="120"
        HorizontalAlignment="Left"
        Background="Black"></StackPanel>
</Grid>

Also, I would not recommend using By property of the DoubleAnimation:
If animation is still running and you click button again, then second animation will be started at wrong position (it will use X value from first animation, which is not finished) - Click button quickly many times and you will see the problem.

Use From and To properties instead.

这篇关于WPF动画:如何使其滑入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆