0

嗨,我正在创建一个应用程序,它有一个在运动后播放的动画,然后它变成空白,我需要一种方法来在动画结束播放声音时在代码中声明,我有声音代码,所以我只需要知道如何为了说明这一点,我正在使用诸如[animation startanimating]and 和[animation stopanimating]and[audioplayer play]之类的代码[audioplayer stop]。谢谢,我是初学者,所以请放轻松:)

4

3 回答 3

1

您可以将代码转换为使用带有完成回调的块。答案中的示例。


编辑示例:

假设您要设置动画的视图称为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
             }

 ];

这是你试过的吗?您可以发布更多实际代码吗?它有助于了解您如何完整地处理动画和回调。

于 2012-07-20T15:57:40.843 回答
0
[UIView animateWithDuration:0.5 
                      delay:0.0 
                    options:UIViewAnimationOptionBeginFromCurrentState 
                 animations:^{
                     //animation block
                     // perform animation here
                 }
                 completion:^(BOOL finished){
                     // play sound here
                     [audioPlayer play]

     }];
于 2012-07-20T16:06:32.847 回答
0

你会想要使用一个CAKeyFrameAnimation. 这将允许您通过一系列具有单独帧时间的图像进行动画处理,在您到达动画结束时获得委托通知,而使用 an 进行动画处理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
    }
}
于 2012-07-20T17:00:50.710 回答