暂停动画旋转,设置角度值,然后再恢复? [英] Pausing an animation rotation, setting the angle value, then resuming later?

查看:121
本文介绍了暂停动画旋转,设置角度值,然后再恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D模型具有恒定旋转动画的情况。当用户触摸屏幕时,我希望停止旋转,并希望用户控制接管旋转的动画。

I have a situation where I have a 3d Model that has a constant rotation animation. When a user touches the screen, I would like the rotation to stop, and the user's control to take over the animation of the rotation.

我试图通过暂停来做到这一点动画,在本地设置旋转的angle属性,然后恢复旋转。但是,我发现由于依赖属性优先级,我的设置值是动画暂停时忽略。

I tried to do this by pausing the animation, setting the angle property of the rotation locally, then resuming the rotation. However, I discovered due to dependency property precedence, my setting values is ignored while the animation is paused.

我能想到的唯一解决方法是用触摸控制相机,而用动画控制实际模型。不幸的是,这会导致其他复杂性,我宁愿两个动作都控制模型本身。

The only workaround I could come up with was to have the touch control the camera, while the animation controls the actual model. Unfortunately this causes other complexities down the line and I'd much rather that both actions control the model itself.

   //In carousel.cs 
   public void RotateModel(double start, double end, int duration)
    {
        RotateTransform3D rt3D = _GroupRotateTransformY;
        Rotation3D r3d = rt3D.Rotation;
        DoubleAnimation anim = new DoubleAnimation();
        anim.From = start;
        anim.To = end;
        anim.BeginTime = null;
        anim.AccelerationRatio = 0.1;
        anim.DecelerationRatio = 0.6;
        anim.Duration = new TimeSpan(0, 0, 0, 0, duration);
        ac = anim.CreateClock();
        ac.Completed += new EventHandler(OnRotateEnded);
        ac.Controller.Begin();
        r3d.ApplyAnimationClock(AxisAngleRotation3D.AngleProperty, ac);

}

    //In another file
    void Carousel_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        var delta = e.DeltaManipulation;         

        RotateTransform3D rt = Carousel.RotateTransform;

        ((AxisAngleRotation3D)rt.Rotation).Angle += (-e.DeltaManipulation.Translation.X/3000) * 360.0;


        e.Handled = true;


    }

    void Carousel_PreviewTouchUp(object sender, TouchEventArgs e)
    {
        Carousel.ResumeRotationAnimation();
    }

    void Carousel_PreviewTouchDown(object sender, TouchEventArgs e)
    {
        Carousel.PauseRotationAnimation();
    }


推荐答案

我遇到过相同的问题需要(也需要3D,也需要Model3DGroup旋转),并且可以这样操作:

I have come across the same need (also 3D, also Model3DGroup rotation) and did it this way:

当动画需要停止时,我获得了动画属性的当前double值(并将其存储本地)。

When the animation needs to stop I get the current double value of the animated property (and store it locally).

var temp = myAxisAngleRotation.Angle;

然后我使用

myAxisAngleRotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, null);

并将动画依赖项属性设置为存储的值。

and set the animated dependency property to the stored value.

myAxisAngleRotation.Angle = temp;

当需要恢复动画时,我将创建一个以当前值开头的新动画。

When the animation needs to resume I create a new animation that starts with the current value.

DoubleAnimation anim = new DoubleAnimation();
anim.From = myAxisAngleRotation.Angle;
anim.To = end;
anim.Duration = new TimeSpan(0, 0, 0, 0, duration);
myAxisAngleRotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, anim);

完成!

如果您想要以恒定的速度制作动画时,您需要在计算持续时间时考虑距离( Math.Abs​​(anim.To-anim.From))。

If you want your animation at a constant speed you will have to take the distance (Math.Abs(anim.To-anim.From)) into account when calculating the duration.

一旦我有了这个。我意识到可以对所有线性动画进行推广,并将其推广为Behavior / AttachedProperty。

Once I had this. I realized this can be generalized for all linear animations and generalized it into a Behavior/AttachedProperty.

这篇关于暂停动画旋转,设置角度值,然后再恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆