UI小项目之拳皇动画的实现(抽取加载图片和播放音乐的方法)

实现思路

1、加载图片

2、播放音乐

 

实现思想

1、封装思想

抽取相同代码生成一个新的方法,通过传递参数调用该方法;

2、内存管理思想

不需要每次调用方法时都重新加载图片,for循环加载图片写在ViewdidLoad中

 

下列代码没有对运行过程中内存管理进行优化

其中加载图片有两种方法:

通过imageNmae加载有缓存

通过imageWithContentsOfFile加载无缓存

有无缓存的区别:

有缓存,使用时不需要重新加载

无缓存,使用时才加载

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//播放器
@property (nonatomic, strong) AVPlayer *player;
//动画
@property (nonatomic, strong) NSArray *standImages;
@property (nonatomic, strong) NSArray *firstSkillImages;
@property (nonatomic, strong) NSArray *secondSkillImages;
@property (nonatomic, strong) NSArray *thirdSkillImages;
@property (nonatomic, strong) NSArray *superSkillImages;
@property (nonatomic, strong) NSArray *deadImages;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    初始化
/ /      加载动画
//    站立
    self.standImages = [self loadImagesWithImagePrefix:@"stand" Count:10];
//    小招
    self.firstSkillImages = [self loadImagesWithImagePrefix:@"xiaozhao1" Count:21];
    self.secondSkillImages = [self loadImagesWithImagePrefix:@"xiaozhao2" Count:35];
    self.thirdSkillImages = [self loadImagesWithImagePrefix:@"xiaozhao3" Count:39];
//    大招
    self.superSkillImages = [self loadImagesWithImagePrefix:@"dazhao" Count:87];
//    死亡
    self.deadImages = [self loadImagesWithImagePrefix:@"dead" Count:23];
//    让人物一开始就处于站立状态
    [self stand];



// 加载音乐    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"stand" withExtension:@"mp3"];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
    self.player = [[AVPlayer alloc] initWithPlayerItem:item];
}


#pragma mark - 功能方法
#pragma mark - 加载图片
- (NSArray *)loadImagesWithImagePrefix:(NSString *)prefix Count:(NSInteger)count
{
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%i", prefix, i + 1];
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [images addObject:image];
    }
    return images;
}
#pragma mark - 执行动画并播放相应的音效
- (void)playZhaoWithImages:(NSArray *)images withSoundName:(NSString *)soundName
{
    //    设置播放的动画
    self.imageView.animationImages = images;
    //    设置播放的次数
    self.imageView.animationRepeatCount = 1;
//    执行动画
    [self.imageView startAnimating];
//    恢复站立
    NSTimeInterval delayTime = 1 / 30.0 *images.count - 0.1;
    [self performSelector:@selector(stand) withObject:nil afterDelay:delayTime];
    
//    播放音乐
    NSURL *url = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"mp3"];
    AVPlayerItem *replaceItem = [[AVPlayerItem alloc] initWithURL:url];
    [self.player replaceCurrentItemWithPlayerItem:replaceItem];
    self.player.rate = 1.5;
}
#pragma mark - 执行动画
#pragma mark - 站立

- (IBAction)stand {
//    设置播放的动画
    self.imageView.animationImages = self.standImages;
//    设置播放的次数
    self.imageView.animationRepeatCount = 0;
//    设置执行的时间
//    self.imageView.animationDuration = 0.8;
    
    [self.imageView startAnimating];
}
#pragma mark - 小招
- (IBAction)firstSkill {
    [self playZhaoWithImages:self.firstSkillImages withSoundName:@"xiaozhao1"];
}
- (IBAction)secondSkill {
    [self playZhaoWithImages:self.secondSkillImages withSoundName:@"xiaozhao2"];
}
- (IBAction)thirdSkill {
    [self playZhaoWithImages:self.thirdSkillImages withSoundName:@"xiaozhao3"];
}

#pragma mark - 大招
- (IBAction)superSkill {
    [self playZhaoWithImages:self.superSkillImages withSoundName:@"dazhao"];
}
#pragma mark - 死亡
- (IBAction)dead {
    //    设置播放的动画
    self.imageView.animationImages = self.deadImages;
    //    设置播放的次数
    self.imageView.animationRepeatCount = 1;
    
    [self.imageView startAnimating];
}
#pragma mark - 游戏结束
- (IBAction)gameOver {
    self.standImages = nil;
    self.firstSkillImages =nil;
    self.secondSkillImages = nil;
    self.thirdSkillImages = nil;
    self.superSkillImages = nil;
    self.deadImages = nil;
    self.player = nil;
    self.imageView.animationImages = nil;
}
@end

 

优化后的代码

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//播放器
@property (nonatomic, strong) AVPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    初始化

//    让人物一开始就处于站立状态
    [self stand];
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"stand" withExtension:@"mp3"];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
    self.player = [[AVPlayer alloc] initWithPlayerItem:item];
}
#pragma mark - 功能方法
#pragma mark - 加载图片
- (void)loadImagesWithImagePrefix:(NSString *)prefix Count:(NSInteger)count
{
//    if ([self.imageView isAnimating]) return;
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%i", prefix, i + 1];
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [images addObject:image];
    }
    //    设置播放的动画
    self.imageView.animationImages = images;
    //    设置播放的次数
    self.imageView.animationRepeatCount = ![prefix isEqualToString:@"stand"];
    
    self.imageView.image = [UIImage imageNamed:@"stand_1"];
    self.imageView.animationDuration = count * 0.04;
    //    执行动画
    [self.imageView startAnimating];
    //    恢复站立
    if ([prefix isEqualToString:@"stand"]) return;
    [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
}
#pragma mark - 执行动画并播放相应的音效
- (void)playMusicWithSoundName:(NSString *)soundName
{
//    播放音乐
    NSURL *url = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"mp3"];
    AVPlayerItem *replaceItem = [[AVPlayerItem alloc] initWithURL:url];
    [self.player replaceCurrentItemWithPlayerItem:replaceItem];
    self.player.rate = 1.5;
}
#pragma mark - 执行动画
#pragma mark - 站立

- (IBAction)stand {
    [self loadImagesWithImagePrefix:@"stand" Count:10];
    [self playMusicWithSoundName:@"stand"];
}
#pragma mark - 小招
- (IBAction)firstSkill {
    [self loadImagesWithImagePrefix:@"xiaozhao1" Count:21];
    [self playMusicWithSoundName:@"xiaozhao1"];
}
- (IBAction)secondSkill {
    }
- (IBAction)thirdSkill {
    
}

#pragma mark - 大招
- (IBAction)superSkill {
    [self loadImagesWithImagePrefix:@"dazhao" Count:87];
    [self playMusicWithSoundName:@"dazhao"];
}
#pragma mark - 死亡
- (IBAction)dead {
}
#pragma mark - 游戏结束
- (IBAction)gameOver {

    self.player = nil;
    self.imageView.animationImages = nil;
}
@end

加载图片

#pragma mark - 加载一系列图片
//传入图片的名称的前缀及图片的数量
- (NSArray *)loadImagesWithImagePrefix:(NSString *)prefix Count:(NSInteger)count
{
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%i", prefix, i + 1];
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [images addObject:image];
    }
    return images;
}

 

加载音乐

#import <AVFoundation/AVFoundation.h>
#pragma mark  - 创建播放器AVPlayer,
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"stand" withExtension:@"mp3"];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
    self.player = [[AVPlayer alloc] initWithPlayerItem:item];
#pragma mark  - 传入要播放的音乐的名称(soundName)然后播放
//    播放音乐
    NSURL *url = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"mp3"];
    AVPlayerItem *replaceItem = [[AVPlayerItem alloc] initWithURL:url];
    [self.player replaceCurrentItemWithPlayerItem:replaceItem];
    self.player.rate = 1.5;

 

posted @ 2015-11-26 21:35  a滴答  阅读(201)  评论(0编辑  收藏  举报