实现思路

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;

转载于:https://www.cnblogs.com/dreamWanweidong/p/4999026.html

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

  1. 微信小程序中使用wxss加载图片并实现动画

    微信小程序中使用wxss加载图片并实现动画 记录微信小程序中使用wxss加载图片并实现动画的方式,最终实现loading效果. 代码 .weui-loading { margin: 0 5px; wi ...

  2. 游戏UI动态加载图片优化

    说到UI优化,很多人对其并不以为意,UI的制作无非使用UGUI或者NGUI.UI优化主要是针对图集,还有一些依赖项的优化,针对的是内存优化,上面这些都是关于静态UI的优化,这个是作为程序员都要经历的阶 ...

  3. Android:ViewPager详解(异步网络加载图片,带图片缓存,并带导航小圆点)

    android 应用中,如欢迎指引页面, 和图片轮播功能, 或者更多的内容在一页显示不了,要分成多个页面,这时候viewpager是很好用的. 首先看下效果: 下面是一个例子,带异步网络加载图片,并带 ...

  4. 【微信小程序】实现小程序下拉刷新与上拉加载

    微信小程序内置的上拉加载.下拉刷新与Android原生的有所不同,Android原生下拉刷新用SwipeRefreshLayout组件,重写onRefresh方法,而上拉加载则是使用RecycleVi ...

  5. html下拉刷新原理,微信小程序 下拉刷新及上拉加载原理解析

    这篇文章主要介绍了微信小程序 下拉刷新及上拉加载实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.下拉刷新的概念及应用场景. 概念: 下拉 ...

  6. html如何添加加载动画效果,CSS3创建加载动画效果

    加载动画在网页设计中是很常见的.用户们都希望网页加载又快又流畅而不是盯着屏幕苦等,而加载动画能够在内容加载完成前给用户视觉反馈,从而能够吸引用户而不让他们直接放弃继续浏览你的网站. 创建加载效果所需的 ...

  7. 【Unity使用UGUI实现王者荣耀UI界面(三)】登录界面以及加载界面优化

    [Unity使用UGUI实现王者荣耀UI界面(三)]登录界面以及加载界面优化 [只是用来玩玩的,不要太当真] 效果显示: zhans 1. 加载界面进度100%跳转登录界面 这个功能好做,只需要将上次 ...

  8. cocos微信小游戏开发-http请求-使用微信云函数-toast-loading-动态加载图片-添加触摸事件-微信分享-label点击事件-背景音乐音效-程序活动状态判断-性能优化

    cocos开发微信小游戏相关-<益智推箱> 扫码查看功能,有需要可直接提问 Cocos Creator 3.4 用户手册 cocos creator基本操作 微信开发文档|云函数 1. h ...

  9. android开发小技巧:实现listview异步加载图片

    2019独角兽企业重金招聘Python工程师标准>>> 针对listview异步加载图片这个问题,麦子学院android开发老师讲了一种非常实用的方法,麦子学院android开发老师 ...

最新文章

  1. java反射最佳实践,java反射性能测试分析
  2. 使用HMTL5 API监控前端性能
  3. 生产环境提升rman备份速度----启动块跟踪
  4. 性能测试方案_MeterSphere案例分享丨基于JMeter的性能测试方案演进之路
  5. TYVJ 1557 MST+LCA
  6. python实时读取日志并打印关键字怎么实现_python pytest测试框架介绍五---日志实时输出...
  7. Updatexml函数再mysql中的作用
  8. C语言ssh软件,如何在C语言程序中使用SSH。
  9. Cisco Packet Tracer 思科模拟器交换机的链路聚合技术
  10. ANSYS win10家庭版安装经验:
  11. IDEA中格式化代码快捷键
  12. Spring 测试运行的时候提示 Unable to find a @SpringBootConfiguration 错误
  13. 名帖77 刘弘珪 楷书《金刚般若波罗蜜经》
  14. 车载以太网协议:SOME/IP (layer5-7)简介
  15. konga--添加service和rouce详细步骤
  16. windows电脑启动问题-0xc000000d
  17. 商业地产数字化转型分析
  18. 我的世界1.13 mod制作——简单的方块(五)
  19. Android调用系统相机拍照像素太低以及内存溢出问题
  20. 动态启动页用gif能实现吗_[前沿科技] 微信启动页变脸的背后,你可知背后的秘密?...

热门文章

  1. 指导软件测试一天200管吃饭两顿,北京来付30车费
  2. Oracle各版本下载集合
  3. Python2参考手册-方法
  4. 蓝牙怎么区分单模和双模_蓝牙中的单模、双模指的是什么意思?有何不同呢?...
  5. 人工智能在计算机视觉方面的应用3000字,人工智能在计算机视觉及网络领域中的应用...
  6. 富格林:掌握现货知识才能避免血汗钱亏损
  7. 【大局观】 NO.4 “加速时代”中怎样成为一个全球化人才?
  8. 按键控制LED灯亮灭——基于arduino单片机
  9. JZOJ(中山纪念中学) 2018.02.02【NOIP普及组】模拟赛D组
  10. android时分转时间截,Android针对时间戳转几时几分昨天前天某年某月某日的显示...