簡體   English   中英

在同一情節提要中多次轉換同一對象

[英]Translate the same object multiple times in the same storyboard

我有一個分為兩部分的動畫,將一個對象滑到屏幕中間,暫停,然后滑到某個位置,將其縮小到零。 運行情節提要時會崩潰,是否不可能對同一情節提要中的同一對象執行兩個不同的transformtransforms?

作為修復,我嘗試將動畫分成兩個情節提要,但是遇到另一個問題。 當第一部分完成時,卡片將重設到角落的起始位置,就像動畫從未發生過一樣。 然后,我的第二個情節提要板在幾秒鍾后開始播放。 如果此后未重置該職位,那就沒問題了。

因此,如何使它像下面那樣在一個情節提要中全部運行,或者如果我將其分割為兩個情節提要,則在完成第1部分動畫后不讓它重置位置。

下面的代碼僅在我從第2部分中刪除第二個transformtransform動畫時才有效。

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

我可以使用關鍵幀在一個故事板上獲得想要的動畫

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM