1

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

  • 我只想在动画运行时播放声音文件。除了对动画的开始和停止时间进行计时,我该怎么做?有没有办法将它与动画执行联系起来?
  • 有没有办法反向运行相同的数组,而不是在拥有两个单独的数组时浪费内存?

** 示例代码

- (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];
}
4

1 回答 1

0

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

** 视图控制器.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

** 视图控制器.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
于 2013-01-28T04:45:52.923 回答