【问题标题】:Translate the same object multiple times in the same storyboard在同一个故事板中多次翻译同一个对象
【发布时间】:2014-02-09 12:20:50
【问题描述】:

我有一个由两部分组成的动画,它将一个对象滑到屏幕中间,暂停,然后滑到某个位置,将其缩小到零。运行情节提要时崩溃,不能对同一个情节提要中的同一个对象进行两次不同的翻译转换吗?

作为修复,我尝试将动画分成两个情节提要,但是我遇到了另一个问题。当第一部分完成时,卡片会重置回角落的起始位置,就好像动画从未发生过一样。然后我的第二个故事板会在几秒钟后启动。如果之后位置没有重置就可以了。

那么我怎样才能让它在一个故事板中运行,如下所示,或者如果我将它分成两个故事板,则在完成第 1 部分动画后不让它重置位置。

下面的代码只有在我从第 2 部分中删除第二个 translatetransform 动画时才有效。

public void SendCardToPile(UIElement bigCard, UIElement targetElement)
{
    this.ZoomDropShadowImage.Visibility = Visibility.Collapsed;

    // setup
    var _Scale = new ScaleTransform
    {
        ScaleX = 1.0,
        ScaleY = 1.0,
        CenterX = 0,
        CenterY = 0
    };

    this.ZoomItemCard.Visibility = Visibility.Visible;

    var _Translate = new TranslateTransform();
    var _Group = new TransformGroup();
    _Group.Children.Add(_Scale);
    _Group.Children.Add(_Translate);
    bigCard.RenderTransform = _Group;

    // animate
    var _Storyboard = new Storyboard { };
    var _Storyboard2 = new Storyboard { };

    var transform = targetElement.TransformToVisual(Application.Current.RootVisual as FrameworkElement);
    Point absolutePosition = transform.Transform(new Point(0, 0));

    // Part 1 **************************

    // translate (location X)
    var _TranslateAnimateX = new DoubleAnimation
    {
        BeginTime = TimeSpan.FromSeconds(0),
        From = -(Application.Current.Host.Content.ActualHeight / 2),
        To = (Application.Current.Host.Content.ActualHeight - 250) / 2,
        Duration = TimeSpan.FromSeconds(.25)
    };
    _Storyboard.Children.Add(_TranslateAnimateX);
    Storyboard.SetTarget(_TranslateAnimateX, _Translate);
    Storyboard.SetTargetProperty(_TranslateAnimateX,
        new PropertyPath(TranslateTransform.XProperty));

    // translate (location Y)
    var _TranslateAnimateY = new DoubleAnimation
    {
        BeginTime = TimeSpan.FromSeconds(0),
        From = (Application.Current.Host.Content.ActualWidth - 400) / 2,
        To = (Application.Current.Host.Content.ActualWidth - 400) / 2,
        Duration = TimeSpan.FromSeconds(.25)
    };
    _Storyboard.Children.Add(_TranslateAnimateY);
    Storyboard.SetTarget(_TranslateAnimateY, _Translate);
    Storyboard.SetTargetProperty(_TranslateAnimateY,
        new PropertyPath(TranslateTransform.YProperty));

    // Part 2 **************************
    double stopDelay = 2;
    double disappearSpeed = .5;

    // scale X
    var _ScaleAnimateX = new DoubleAnimation
    {
        BeginTime = TimeSpan.FromSeconds(stopDelay),
        To = 0.0,
        From = 1.0,
        Duration = TimeSpan.FromSeconds(disappearSpeed)
    };
    _Storyboard.Children.Add(_ScaleAnimateX);
    Storyboard.SetTarget(_ScaleAnimateX, _Scale);
    Storyboard.SetTargetProperty(_ScaleAnimateX,
        new PropertyPath(ScaleTransform.ScaleXProperty));

    // scale Y
    var _ScaleAnimateY = new DoubleAnimation
    {
        BeginTime = TimeSpan.FromSeconds(stopDelay),
        To = 0.0,
        From = 1.0,
        Duration = TimeSpan.FromSeconds(disappearSpeed)
    };
    _Storyboard.Children.Add(_ScaleAnimateY);
    Storyboard.SetTarget(_ScaleAnimateY, _Scale);
    Storyboard.SetTargetProperty(_ScaleAnimateY,
        new PropertyPath(ScaleTransform.ScaleYProperty));

    // translate (location X)
    var _TranslateAnimateX2 = new DoubleAnimation
    {
        BeginTime = TimeSpan.FromSeconds(stopDelay),
        To = absolutePosition.X,
        Duration = TimeSpan.FromSeconds(disappearSpeed)
    };
    _Storyboard.Children.Add(_TranslateAnimateX2);
    Storyboard.SetTarget(_TranslateAnimateX2, _Translate);
    Storyboard.SetTargetProperty(_TranslateAnimateX2,
        new PropertyPath(TranslateTransform.XProperty));

    // translate (location Y)
    var _TranslateAnimateY2 = new DoubleAnimation
    {
        BeginTime = TimeSpan.FromSeconds(stopDelay),
        To = absolutePosition.Y,
        Duration = TimeSpan.FromSeconds(disappearSpeed)
    };
    _Storyboard.Children.Add(_TranslateAnimateY2);
    Storyboard.SetTarget(_TranslateAnimateY2, _Translate);
    Storyboard.SetTargetProperty(_TranslateAnimateY2,
        new PropertyPath(TranslateTransform.YProperty));

    // finalize
    //EventHandler eh = null;
    //eh = (s, args) =>
    //{
    //    _Storyboard.Completed -= eh;
    //};
    //_Storyboard.Completed += eh;

    _Storyboard.Begin();
}

【问题讨论】:

  • It crashes when running the storyboard, is it not possible to do two different translatetransforms on the same object in the same storyboard? 是的,这是可能的。如果您可以发布崩溃时看到的确切错误,将会有所帮助。
  • 它并没有真正提供任何有用的东西。它只是在 storyboard.begin 处中断: System.Windows.ni.dll 中发生了“System.InvalidOperationException”类型的异常,但未在用户代码中处理

标签: c# wpf


【解决方案1】:

我能够使用关键帧通过单个故事板获得我想要的动画

    public void SendCardToPile(UIElement bigCard, UIElement targetElement)
    {
        this.ZoomDropShadowImage.Visibility = Visibility.Collapsed;

        double stopDelay = 2;
        double disappearSpeed = .5;

        // setup
        var _Scale = new ScaleTransform
        {
            ScaleX = 1.0,
            ScaleY = 1.0,
            CenterX = 0,
            CenterY = 0
        };

        this.ZoomItemCard.Visibility = Visibility.Visible;

        var _Translate = new TranslateTransform();
        var _Group = new TransformGroup();
        _Group.Children.Add(_Scale);
        _Group.Children.Add(_Translate);
        bigCard.RenderTransform = _Group;

        // animate
        var _Storyboard = new Storyboard { };

        var transform = targetElement.TransformToVisual(Application.Current.RootVisual as FrameworkElement);
        Point absolutePosition = transform.Transform(new Point(0, 0));

        // translate (location X)
        DoubleAnimationUsingKeyFrames _TranslateAnimateX = new DoubleAnimationUsingKeyFrames();

        System.Windows.Media.Animation.Storyboard.SetTarget(_TranslateAnimateX, _Translate);
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(_TranslateAnimateX, new PropertyPath(TranslateTransform.XProperty));

        _TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)),
            Value = -(Application.Current.Host.Content.ActualHeight / 2),
        });

        _TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.25)),
            Value = (Application.Current.Host.Content.ActualHeight - 250) / 2
        });

        _TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
            Value = (Application.Current.Host.Content.ActualHeight - 250) / 2
        });

        _TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
            Value = absolutePosition.X
        });

        _Storyboard.Children.Add(_TranslateAnimateX);

        // translate (location Y)
        DoubleAnimationUsingKeyFrames _TranslateAnimateY = new DoubleAnimationUsingKeyFrames();

        System.Windows.Media.Animation.Storyboard.SetTarget(_TranslateAnimateY, _Translate);
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(_TranslateAnimateY, new PropertyPath(TranslateTransform.YProperty));

        _TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)),
            Value = (Application.Current.Host.Content.ActualWidth - 400) / 2
        });

        _TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.25)),
            Value = (Application.Current.Host.Content.ActualWidth - 400) / 2
        });

        _TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
            Value = (Application.Current.Host.Content.ActualWidth - 400) / 2
        });

        _TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
            Value = absolutePosition.Y
        });

        _Storyboard.Children.Add(_TranslateAnimateY);

        // scale X
        DoubleAnimationUsingKeyFrames _ScaleAnimateX = new DoubleAnimationUsingKeyFrames();

        System.Windows.Media.Animation.Storyboard.SetTarget(_ScaleAnimateX, _Scale);
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(_ScaleAnimateX, new PropertyPath(ScaleTransform.ScaleXProperty));

        _ScaleAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
            Value = 1.0
        });

        _ScaleAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
            Value = 0.0
        });

        _Storyboard.Children.Add(_ScaleAnimateX);

        // scale Y
        DoubleAnimationUsingKeyFrames _ScaleAnimateY = new DoubleAnimationUsingKeyFrames();

        System.Windows.Media.Animation.Storyboard.SetTarget(_ScaleAnimateY, _Scale);
        System.Windows.Media.Animation.Storyboard.SetTargetProperty(_ScaleAnimateY, new PropertyPath(ScaleTransform.ScaleYProperty));

        _ScaleAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
            Value = 1.0
        });

        _ScaleAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
            Value = 0.0
        });

        _Storyboard.Children.Add(_ScaleAnimateY);

        // finalize
        //EventHandler eh = null;
        //eh = (s, args) =>
        //{
        //    _Storyboard.Completed -= eh;
        //};
        //_Storyboard.Completed += eh;


        _Storyboard.Begin();
    }

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2013-09-17
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多