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

时间:2012-02-27 15:43:41

标签: ios wpf animation

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

1 个答案:

答案 0 :(得分: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>