如何说明动画的结束?

时间:2012-07-20 15:55:23

标签: iphone objective-c xcode animation uiviewanimation

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

3 个答案:

答案 0 :(得分: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
             }

 ];

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

答案 1 :(得分:0)

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

     }];

答案 2 :(得分: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
    }
}