2010-02-09 22 views
4

我尝试向我建立的用户控件添加翻转动画。用户控件很简单,它有一个87x87的图像正面和背面以及一些属性。假设它代表我正在为乐趣而工作的游戏中的一块拼图。我正在尝试为用户从卡组中挑选瓦片的翻转效果进行动画处理。我觉得我需要通过代码而不是xaml来做到这一点,原因有两个:1.瓦片翻转之后还有另一个转换来旋转瓦片(当前正在工作)。2.瓦片翻转后,我想解除事件。完全通过代码执行翻转动画WPF

我得到的问题只是方法退出后最后一次动画运行。 我想我需要一个故事板,但我看着糊涂了两种方式的所有例子:

如何改变形象中期故事板,我设置targetProperty是 我一直在努力关闭这些是什么做两个博客。

http://www.codeguru.com/csharp/csharp/cs_misc/userinterface/article.php/c12221 http://blogs.msdn.com/tess/archive/2009/03/16/silverlight-wpf-flipimage-animation.aspx

public void FlipFront() 
    { 
      DoubleAnimation flipfront = new DoubleAnimation(0, 90, new Duration(new  TimeSpan(0, 0, 1))); 
     SkewTransform skew = new SkewTransform(); 
     this.RenderTransform = skew; 
     skew.BeginAnimation(SkewTransform.AngleYProperty, flipfront);   


    } 

    public void FlipBack() 
    { 

     ImageSourceConverter source = new ImageSourceConverter(); 
     this.ImageFace.Source = new BitmapImage(new Uri("Back.jpg", UriKind.Relative)); 

     DoubleAnimation flipfront = new DoubleAnimation(90, 0, new Duration(new TimeSpan(0, 0, 1))); 
     SkewTransform skew = new SkewTransform(); 
     this.RenderTransform = skew; 
     skew.BeginAnimation(SkewTransform.AngleYProperty, flipfront); 
    } 

    public void Flip() 
    { 
     FlipFront(); 
     FlipBack(); 
    } 

我打破了翻转成两个独立的方法,因为我虽然这将有助于解决我遇到的问题。

回答

1

哇,这并没有在天长的时间更新...以防万一任何人的追踪这一个:

的问题是你不等待“翻转前”动画之前立即完成开始“翻转” - 现在,因为您基本上是强制将Y角动画立即跳到90度,这就是为什么它看起来不能正确触发。

有很多方法可以解决这个问题 - 第一件让人想到的事情是DoubleAnimation有一个方法叫做CreateClock,它会返回一个AnimationClock对象。该对象上有一个Completed事件,它会告诉你何时该动画“完成”。附上一个处理程序(记住你要分离它,以免泄漏内存),并在那里调用你的“开始翻转”方法。我一起扔了一些效率很低的东西,但它会显示原理:

public AnimationClock StartFlipFrontAnimation() 
{ 
    this.ImageFace.Source = _frontFace; 
    DoubleAnimation flipfront = new DoubleAnimation(0, 90, new Duration(new TimeSpan(0, 0, 3))); 
    SkewTransform skew = new SkewTransform(); 
    this.RenderTransform = skew; 
    skew.BeginAnimation(SkewTransform.AngleYProperty, flipfront); 
    return flipfront.CreateClock(); 
} 

public AnimationClock StartFlipBackAnimation() 
{ 
    this.ImageFace.Source = _backFace; 
    DoubleAnimation flipfront = new DoubleAnimation(90, 0, new Duration(new TimeSpan(0, 0, 3))); 
    SkewTransform skew = new SkewTransform(); 
    this.RenderTransform = skew; 
    skew.BeginAnimation(SkewTransform.AngleYProperty, flipfront); 
    return flipfront.CreateClock(); 
} 

public void BeginFlip() 
{  
    var frontClk = StartFlipFrontAnimation();  
    frontClk.Completed += FrontFlipDone;   
} 

private void FrontFlipDone(object sender, EventArgs args) 
{ 
    var clk = sender as AnimationClock; 
    if(clk != null) 
    { 
     clk.Completed -= FrontFlipDone; 
    } 
    var backClk = StartFlipBackAnimation(); 
}