实现一个动画
在mainpage里面,点击按钮 ,一个usercontrol从下到上慢慢浮现,再点击一下,再从下到上慢慢隐藏,我本来想用渐变工具把usercontrol分成几块 然后调透明度,可是用渐变工具根本就没有分层,怎么弄,求个思路。
[解决办法]
不用什么渐变工具,用Storyboard,控制透明度和RenderTransform就可以。
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="5"/>
<RowDefinition Height="45"/>
</Grid.RowDefinitions>
<Grid.Resources>
<Storyboard x:Name="storyboard1">
<DoubleAnimation Duration="0:0:3" To="-100" Storyboard.TargetName="test" Storyboard.TargetProperty="Y"/>
<DoubleAnimation Duration="0:0:3" To="1" Storyboard.TargetName="btn1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
<Storyboard x:Name="storyboard2">
<DoubleAnimation Duration="0:0:3" To="0" Storyboard.TargetName="test" Storyboard.TargetProperty="Y"/>
<DoubleAnimation Duration="0:0:3" To="0" Storyboard.TargetName="btn1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</Grid.Resources>
<Button x:Name="btn1" FontSize="15" Content="O(∩_∩)O" HorizontalAlignment="Center" VerticalAlignment="Bottom" Opacity="0" Margin="157,0,163,10">
<Button.RenderTransform>
<TranslateTransform x:Name="test"/>
</Button.RenderTransform>
</Button>
<Button FontSize="15" Content="启动动画" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click" />
</Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
private Int32 i = 0;
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
i++;
if (i % 2 == 0) {
storyboard2.Begin();
} else {
storyboard1.Begin();
}
}
}
}