2012-07-20 78 views
0

嗨,我正在創建一個應用程序,其中有一個動畫後播放一個動畫,然後它變成空白,我需要一種方式來聲明動畫結束播放聲音時,我有聲音代碼,所以我只需要知道如何陳述它,我正在使用代碼,如[animation startanimating][animation stopanimating],[audioplayer play][audioplayer stop]。謝謝我是初學者,所以請去容易:)如何聲明動畫結束?

+0

安置自己的整個代碼請。 – 2012-07-20 16:20:27

+0

http://pastebin.com/x9m9qXcb – user1483652 2012-07-20 16:33:56

+0

你究竟想用動畫來完成什麼?你想每秒翻閱一張圖片嗎? – 2012-07-20 16:35:24

回答

1

您可以轉換您的代碼使用一個完成回調塊。 this答案中的示例。


編輯與例如:

比方說,你想動畫的觀點被稱爲myView

[UIView animateWithDuration:0.5 
        delay:0.0 
       options:UIViewAnimationOptionBeginFromCurrentState 
      animations:^{ 
       myView.alpha = 0; // a fade out animation 
      } 
      completion:^(BOOL finished) 
      { 
       [audioPlayer play]; 
       // whatever else 
      } 

]; 

這是你嘗試過什麼?你能發佈更多的實際代碼嗎?它有助於瞭解您如何處理整個動畫和回調。

+0

嘿,正如我所說我是一個總的初學者和我試過這樣做,但它不起作用,可能是因爲我沒有做對嗎? – user1483652 2012-07-20 16:03:57

+0

這是我完成它的方式:^(BOOL finished){audioPlayer play]; play.hidden = 0; }; – user1483652 2012-07-20 16:04:36

+0

好吧,我會創建一個粘貼bin並把鏈接放在這裏幾分鐘,它會有整個代碼:) – user1483652 2012-07-20 16:22:01

0
[UIView animateWithDuration:0.5 
         delay:0.0 
        options:UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        //animation block 
        // perform animation here 
       } 
       completion:^(BOOL finished){ 
        // play sound here 
        [audioPlayer play] 

    }]; 
+0

我應該把這個放在我的動畫代碼中嗎? – user1483652 2012-07-20 16:08:02

+0

這段代碼是針對動畫的......所以當你想要執行動畫時寫下它...... – 2012-07-20 18:17:30

0

您打算使用CAKeyFrameAnimation。這將允許您通過一系列具有單獨幀時間的圖像生成動畫效果當您到達動畫結尾時會獲得委託通知,這些動畫將使用UIImageView進行動畫處理,但不會提供此動畫。

一些示例代碼讓你開始(假設ARC):

// create an animation overlay view to display and animate our image frames 
UIView *animationView = [[UIView alloc] initWithFrame:frame]; 
[self.view addSubview:animationView]; 

// using 4 images for the example 
NSArray *values = [NSArray arrayWithObjects:(id)[[UIImage imageNamed:@"Image1.png"] CGImage], 
         (id)[[UIImage imageNamed:@"Image2.png"] CGImage], 
         (id)[[UIImage imageNamed:@"Image3.png"] CGImage], 
         (id)[[UIImage imageNamed:@"Image4.png"] CGImage], 
         nil]; 

// change the times as you need 
NSArray *keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], 
         [NSNumber numberWithFloat:0.25], 
         [NSNumber numberWithFloat:0.50], 
         [NSNumber numberWithFloat:1.0],nil]; 

// animating the contents property of the layer 
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; 
keyframe.values = values; 
keyframe.keyTimes = keyTimes; 
keyframe.calculationMode = kCAAnimationLinear; 
keyframe.duration = 1.0; // 1 second, can be whatever you want, obviously 
keyframe.delegate = self; 
keyframe.removedOnCompletion = NO; 
keyframe.fillMode = kCAFillModeForwards; 

// "user defined string" can be whatever you want and probably should be constant somewhere in 
// your source. You can use this same key if you want to remove the animation 
// later using -[CALayer removeAnimationForKey:] on the animationView's layer 
[animationView.layer addAnimation:keyframe forKey:@"user defined string"]; 

然後,別的地方,實現您的委託方法:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished 
{ 
    if (finished) { 
     // play music 
    } 
}