Wpf动画 - 就像在iOS上摇动图标一样

Lir*_*ess 0 wpf animation ios

我希望在wpf中有一个图像动画,其中图像轻微抖动以便删除它,就像iOS中的应用程序一样.你知道一些有用的东西吗?已经建成的东西?非常感谢.

Phi*_*hil 6

这是一个摇动按钮文本的完整示例.您应该能够调整它以抖动图像,并使用缓动功能对其进行改进.

        <Grid.Resources>
            <ControlTemplate x:Key="ShakingButtonTemplate" TargetType="Button">
                <Border Margin="5" BorderBrush="Aquamarine" BorderThickness="5" CornerRadius="5">
                    <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding Content}">
                        <ContentPresenter.RenderTransform>
                            <TransformGroup>
                                <TranslateTransform x:Name="Position"/>
                            </TransformGroup>
                        </ContentPresenter.RenderTransform>
                    </ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="ShakeIt">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames
                                        Storyboard.TargetName="Position" 
                                        Storyboard.TargetProperty="X" 
                                        RepeatBehavior="5x"
                                        >
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.05" Value="0"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="2"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.20" Value="-2"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0"/>
                                    </DoubleAnimationUsingKeyFrames>
<!--
                                    <DoubleAnimation 
                                        Storyboard.TargetName="Position" 
                                        Storyboard.TargetProperty="X" 
                                        From="-2" To="2" 
                                        Duration="0:0:0:0.1" 
                                        AutoReverse="True" 
                                        RepeatBehavior="10x">
                                    </DoubleAnimation>
-->
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

            <Style x:Key="ShakingButton" TargetType="Button">
                <Setter Property="Template" Value="{StaticResource ShakingButtonTemplate}"/>
            </Style>
        </Grid.Resources>

        <StackPanel>
            <Button Style="{StaticResource ShakingButton}" Content="This is a button" />
        </StackPanel>
Run Code Online (Sandbox Code Playgroud)