Animation in codebehind for loop, using RenderTransform

久未见 提交于 2020-01-03 03:13:08

问题


Is it possible to animate the "old school" way, in codebehind, instead of xaml?

I just want an arrow that points to something with a 'bounce effect' which I could easily do in my own for loop. But I do not know how to refresh or do a timer delay, inside the loop. I already placed the image into position in codebehind. All I want to do is this simple animation...

public void validationArrow()
{

    var validationArrow = new Image();
    validationArrow.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
    LayoutRoot.Children.Add(validationArrow);
    validationArrow.Stretch = Stretch.None;
    validationArrow.VerticalAlignment = System.Windows.VerticalAlignment.Top;
    validationArrow.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
    var arrowPosition = new TranslateTransform { X = 0, Y = 0 };
    validationArrow.RenderTransform = arrowPosition;

    validationArrow.Name = "validationArrow";

    for (int i = 150; i >= 0; i--)
    {
        arrowPosition.X = i;
        validationArrow.RenderTransform = arrowPosition;
        // how can I refresh screen and do some timing here?
    }
}

回答1:


There's no school like the old school ;) Here, this should help you on your way. You can play with the millisecond and Y translation values being passed to the BuildEasing method to change the 'bounce' effect's speed and distance.

    private void RunStoryboard()
    {
        var arrowImage = new Image();
        arrowImage.RenderTransform = new CompositeTransform();
        arrowImage.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
        LayoutRoot.Children.Add(arrowImage);

        Storyboard storyboard = new Storyboard();
        storyboard.Children.Add(BuildKeyFrame(arrowImage));
        storyboard.Begin();
    }

    private DoubleAnimationUsingKeyFrames BuildKeyFrame(Image target)
    {
        DoubleAnimationUsingKeyFrames kf = new DoubleAnimationUsingKeyFrames();
        Storyboard.SetTarget(kf, target);
        Storyboard.SetTargetProperty(kf, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));

        kf.KeyFrames.Add(BuildEasing(100, 10));
        kf.KeyFrames.Add(BuildEasing(200, 0));
        kf.KeyFrames.Add(BuildEasing(300, 10));
        kf.KeyFrames.Add(BuildEasing(400, 0));
        kf.KeyFrames.Add(BuildEasing(500, 10));
        kf.KeyFrames.Add(BuildEasing(600, 0));

        return kf;
    }

    private EasingDoubleKeyFrame BuildEasing(int ms, int value)
    {
        return new EasingDoubleKeyFrame()
        {
            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, ms)),
            Value = value
        };
    }


来源:https://stackoverflow.com/questions/10115657/animation-in-codebehind-for-loop-using-rendertransform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!