我想将声音文件的播放链接到动画执行

时间:2013-01-11 15:24:12

标签: iphone xcode animation audio

我在这里有一个滚动开口的小动画,然后我只是反转它以使滚动再次关闭。我有两个问题。

  • 我想在动画播放时播放声音文件 运行。在开始和停止计时之外我该如何做到这一点 动画的时代?有没有办法将它与动画联系起来 执行?
  • 有没有办法反向运行相同的数组,而不是浪费 内存有两个独立的数组?

**示例代码

- (void)loadAnimationArray;
{
    // scroll close frame order
    animationclose=[[NSArray alloc] initWithObjects:
                    [UIImage imageNamed:@"frame1.png"],
                    [UIImage imageNamed:@"frame2.png"],
                    [UIImage imageNamed:@"frame3.png"],

                    ...

                    [UIImage imageNamed:@"frame25.png"],
                    [UIImage imageNamed:@"frame26.png"],nil];

    // scroll open order
    animationopen=[[NSArray alloc] initWithObjects:
                   [UIImage imageNamed:@"frame26.png"],
                   [UIImage imageNamed:@"frame25.png"],
                   [UIImage imageNamed:@"frame24.png"],

                   ...

                   [UIImage imageNamed:@"frame3.png"],
                   [UIImage imageNamed:@"frame2.png"],
                   [UIImage imageNamed:@"frame1.png"],nil];
}

- (IBAction)buttonOpen:(id)sender
{
    // default image post animation
    _imageScrollAnimation.image = [UIImage imageNamed:@"frame1.png"];

    // setting animation parameters
    self.imageScrollAnimation.animationDuration=3;
    self.imageScrollAnimation.animationRepeatCount=1;
    self.imageScrollAnimation.animationImages=animationopen;
    [self.imageScrollAnimation startAnimating];
}

- (IBAction)buttonClose:(id)sender
{
    // default image post animation
    _imageScrollAnimation.image = [UIImage imageNamed:@"frame26.png"];

    // setting animation parameters
    self.imageScrollAnimation.animationDuration=3;
    self.imageScrollAnimation.animationRepeatCount=1;
    self.imageScrollAnimation.animationImages=animationclose;
    [self.imageScrollAnimation startAnimating];
}

1 个答案:

答案 0 :(得分:0)

好吧,我最终不得不建立自己的原始动画播放器。我们的想法是根据需要打开和关闭动画卷轴,并在后台运行纸张滚动声音。基本上,我使用一个按钮启动一个计时器,该计时器贯穿动画帧并开始播放声音文件。当帧数达到指定的水平时,我用它来停止计时器和播放声音。

** ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
@public

@private
    BOOL scroll_open;
    int animationCount;
    NSURL *url;
    NSTimer *imageTimer;
}

@property (strong, nonatomic) IBOutlet UIImageView *imageTextPortal;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

-(IBAction)buttonCloseScroll:(id)sender;
-(IBAction)buttonOpenScroll:(id)sender;
-(void)startCloseScrollAnimation;
-(void)startOpenScrollAnimation;
-(void)changeOpenScrollImage;
-(void)changeCloseScrollImage;

@end

** ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize imageTextPortal;

-(IBAction)buttonOpenScroll:(id)sender
{
    // starting timed open scroll sequence
    [self startOpenScrollAnimation];
}

-(IBAction)buttonCloseScroll:(id)sender
{
    // starting timed close scroll sequence
    [self startCloseScrollAnimation];
}

-(void)startOpenScrollAnimation
{
    // playing the sound
    NSError *error;
    url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"rolling" ofType:@"mp3"]];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    _audioPlayer.delegate = self;
    _audioPlayer.volume = 1.0;
    [_audioPlayer play];

    // setting animation count for decrement
    animationCount = 26;

    // starting timer
    imageTimer = [NSTimer scheduledTimerWithTimeInterval:.035 target:self selector:@selector(changeOpenScrollImage) userInfo:nil repeats:YES];
}

-(void)changeOpenScrollImage
{
    // switching image
    self.imageTextPortal.image = [UIImage imageNamed:[NSString stringWithFormat:@"frame%i.png" , animationCount]];

    // decrementing image count
    --animationCount;

    // testing for end of animation
    if (animationCount < 1)
    {
        // turning off timer
        [imageTimer invalidate];
        imageTimer = nil;

        // stopping sound play
        [_audioPlayer stop];

        // setting default image after the animation
        imageTextPortal.image = [UIImage imageNamed:@"frame1.png"];
    }
}

-(void)startCloseScrollAnimation
{
    // playing the sound
    NSError *error;
    url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"rolling" ofType:@"mp3"]];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    _audioPlayer.delegate = self;
    _audioPlayer.volume = 1.0;
    [_audioPlayer play];

    // setting animation count for decrement
    animationCount = 1;

    // starting timer
    imageTimer = [NSTimer scheduledTimerWithTimeInterval:.035 target:self selector:@selector(changeCloseScrollImage) userInfo:nil repeats:YES];
}

-(void)changeCloseScrollImage
{
    // switching image
    self.imageTextPortal.image = [UIImage imageNamed:[NSString stringWithFormat:@"frame%i.png" , animationCount]];

    // incrementing image count
    ++animationCount;

    // testing for end of animation
    if (animationCount > 26)
    {
        // turning off timer
        [imageTimer invalidate];
        imageTimer = nil;

        // stopping sound
        [_audioPlayer stop];

        // setting default image after the animation
        imageTextPortal.image = [UIImage imageNamed:@"frame26.png"];
    }
}

@end