开发者

Performing a Flip animation completely through code WPF

I am try to add a flip animation to a user control I built. The user control is simple it has a 87x87 image front and back and some properties. It is suppose to represent a tile in a game I am working on for fun. I am trying to animate a flipping affect of the user picking the tile from the deck. I feel I need to do this through code instead of xaml for two reasons: 1. There is another transform after the tile is flip to rotate the tile (currently working) 2. After the tile is flipped I want to unhook the event.

The issue that I am getting is only the last animation runs after the method has exited. I think I need a Storyboard but all the examples I looked at confused me in two ways:

How do I change the image mid story board, and what do I set the targetProperty to be I have been working off these two blogs.

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.BeginAnima开发者_JAVA百科tion(SkewTransform.AngleYProperty, flipfront); 
    }

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

I broke flip into two separate methods because I though it would help fix the issue I am experiencing.


Wow, this hasn't been updated in a loong time...just in case anybody's tracking this one:

The problem is you're not waiting for the "flip front" animation to complete before immediately starting the "flip back" - now since you're basically force-jumping the Y angle animation immediately to 90 degrees, that's why it looks like it's not firing properly.

There are a LOT of ways you can work around this - the first thing that jumps to mind is that the DoubleAnimations have a method on them called CreateClock, which will return you back an AnimationClock object. That object has a Completed event on it, which will tell you when that animation is "done". Attach a handler (remember you'll want to detach it lest you leak memory), and call your "start flipping to back" method there. I've thrown something very inefficient together, but it'll show the principle:

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜