嗨,我正在創建一個應用程序,其中有一個動畫後播放一個動畫,然後它變成空白,我需要一種方式來聲明動畫結束播放聲音時,我有聲音代碼,所以我只需要知道如何陳述它,我正在使用代碼,如[animation startanimating]
和[animation stopanimating]
,[audioplayer play]
和[audioplayer stop]
。謝謝我是初學者,所以請去容易:)如何聲明動畫結束?
回答
您可以轉換您的代碼使用一個完成回調塊。 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
}
];
這是你嘗試過什麼?你能發佈更多的實際代碼嗎?它有助於瞭解您如何處理整個動畫和回調。
嘿,正如我所說我是一個總的初學者和我試過這樣做,但它不起作用,可能是因爲我沒有做對嗎? – user1483652 2012-07-20 16:03:57
這是我完成它的方式:^(BOOL finished){audioPlayer play]; play.hidden = 0; }; – user1483652 2012-07-20 16:04:36
好吧,我會創建一個粘貼bin並把鏈接放在這裏幾分鐘,它會有整個代碼:) – user1483652 2012-07-20 16:22:01
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
//animation block
// perform animation here
}
completion:^(BOOL finished){
// play sound here
[audioPlayer play]
}];
我應該把這個放在我的動畫代碼中嗎? – user1483652 2012-07-20 16:08:02
這段代碼是針對動畫的......所以當你想要執行動畫時寫下它...... – 2012-07-20 18:17:30
您打算使用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
}
}
- 1. 如果聲明結束
- 2. 預期的聲明結束
- 3. 期待聲明結束
- 4. 預計:聲明結束
- 5. 意外的聲明結束
- 6. 聲明gameObject動畫
- 7. 如何開始動畫結束活動
- 8. 如何播放一次聲音或直到動畫結束?
- 9. 否則如果聲明結束程序
- 10. 如何處理佈局動畫結束
- 11. 如何獲取動畫結束通知
- 12. 如何突然結束動畫?
- 13. 如何通過動畫結束UILongPressGestureRecognizer?
- 14. UITableView,如何捕捉動畫結束?
- 15. 如何結束動畫? (處理)
- 16. 如何在另一個動畫結束時啓動動畫?
- 17. 如何聲明'動畫停止動畫'的命令?
- 18. 當動畫結束時執行動畫
- 19. 用動畫GIF結束Flash動畫
- 20. 執行動畫動畫結束時
- 21. 輸入結束時的預期聲明或聲明C
- 22. 輸入結束時的預期聲明或聲明 - 錯誤
- 23. 預期聲明或聲明在輸入結束?
- 24. 如何在Android中動畫結束後刪除動畫視圖?
- 25. 如何在佈局動畫結束時觸發動畫
- 26. 預計結束聲明。 VB.NET Razor
- 27. 意外的聲明結束 - 在android AIDE
- 28. 我得到BC30205:預計聲明結束
- 29. 中聲明,開始,結束語句
- 30. 條件滿足後結束CASE聲明
安置自己的整個代碼請。 – 2012-07-20 16:20:27
http://pastebin.com/x9m9qXcb – user1483652 2012-07-20 16:33:56
你究竟想用動畫來完成什麼?你想每秒翻閱一張圖片嗎? – 2012-07-20 16:35:24