2010-09-09 76 views
0

我有一個具有恆定旋轉動畫的3D模型的情況。當用戶觸摸屏幕時,我想要停止旋轉,並且用戶的控件接管旋轉的動畫。暫停動畫旋轉,設置角度值,然後再恢復?

我試圖通過暫停動畫,在本地設置旋轉角度屬性,然後恢復旋轉。但是,由於dependency property precedence,我發現我的設置值在動畫暫停時被忽略。

我能想出的唯一解決方法是讓觸摸控制相機,而動畫控制實際模型。不幸的是,這導致了其他複雜性,我寧願這兩個行爲都控制模型本身。

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

回答

1

我也碰到過同樣需要(也3D,也Model3DGroup旋轉),做這樣說:當動畫需要停止我得到當前

動畫屬性的兩倍值(並將其存儲在本地)。

var temp = myAxisAngleRotation.Angle; 

我然後使用

myAxisAngleRotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, null); 

依賴屬性移除動畫和動畫的依賴屬性設置爲所存儲的值。

myAxisAngleRotation.Angle = temp; 

當動畫需要恢復時,我創建一個以當前值開始的新動畫。

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))。

一旦我有了這個。我意識到這可以推廣到所有線性動畫,並將其推廣到Behavior/AttachedProperty中。