2010-02-08 52 views
1

我的代碼工作,我的動畫,但我不知道如何做到這一點:在動畫集結束時在MonoTouch中實現CAKeyFrameAnimation回調?

Animation End Callback for CALayer?

...在MonoTouch的。

這裏就是我已經有了:

public partial class MyCustomView: UIView 
{ 
    // Code Code Code 

    private void RunAnimation() 
    {   
     CAKeyFrameAnimation pathAnimation = new CAKeyFrameAnimation(); 
     pathAnimation.KeyPath = "position"; 
     pathAnimation.Path = trail; //A collection of PointFs 
     pathAnimation.Duration = playbackSpeed; 
     Character.Layer.AddAnimation(pathAnimation,"MoveCharacter"); 
    } 

    //Code Code Code 
} 

MyCustomView已經從UIView的繼承和我無法從兩個類繼承。當完成動畫時,如何調用名爲「AnimationsComplete()」的函數?

回答

4

我不是在我的開發機器前,但我認爲你創建一個從CAAnimationDelegate降序的類,實現「void AnimationStopped(CAAnimation anim,bool finished)方法」,然後將該類的一個實例分配給pathAnimation .Delegate(在你的示例中)。

所以,這樣的事情(警告 - 未經測試的代碼):

public partial class MyCustomView: UIView 
{ 
    private void RunAnimation() 
    {   
     CAKeyFrameAnimation pathAnimation = new CAKeyFrameAnimation(); 

     // More code. 

     pathAnimation.Delegate = new MyCustomViewDelegate(); 

    } 
} 

public class MyCustomViewDelegate : CAAnimationDelegate 
{ 
    public void AnimationStopped(CAAnimation anim, bool finished) 
    { 
     // More code. 
    } 
} 
+0

好吧,這是我不能正確解釋這個故障。問題是我使用的類已經從UIView繼承,所以我不能從兩個類繼承。我會更新我的問題。 – Abel 2010-02-08 16:25:47

+1

我認爲這不重要。 CAAnimationDelegate將是一個*獨立的*類。所以,你將擁有MyCustomView和MyCustomViewDelegate類。然後,在RunAnimation()中,您將擁有pathAnimation.Delegate = new MyCustomViewDelegate(); (例如)。 – dommer 2010-02-08 18:37:51

+0

你完全正確。謝謝您的幫助! – Abel 2010-02-09 04:22:54