2012-07-20 61 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 
    } 
}