实现思路

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. 1106 Lowest Price in Supply Chain
  2. poi 拆分带图片的word_学会这2招,再多的“表格编号”都能轻松解决!【Word教程】...
  3. 小学生计算机课堂实践的重要性,多媒体在小学教学中的重要性
  4. Delphi 2009 超前预知!
  5. Flex itemReanderer(转)
  6. Linux/Ubuntu 单机配置Hbase
  7. [python]网络编程基础学习笔记(一)客户/服务器网络介绍
  8. 网络流 增广路 入门很好的文章
  9. 【GDB调试学习笔记】 Makefie上
  10. C#实现在图片上斜着写字
  11. 企业盈利能力分析-毛利率、销售净利率、投资回报率、权益回报率、资产回报率...
  12. CDN网站加速的工作流程
  13. 小红书种草模式有哪些?如何保证种草效果
  14. 融合算法性能评价指标
  15. 352记--扬哥本纪
  16. 微软专业资历认证有哪些?
  17. python使用eyed3获取音频信息包含采样率比特率和通道信息等
  18. MySQL储存过程详解
  19. 计算机程序是怎样运行的
  20. 使用install shield制作安装程序问题集锦

热门文章

  1. 区块链+智能投顾的可行性初探
  2. matlab中syms怎么替代,科学网—Matlab中的syms与conj - 孔令才的博文
  3. Linux内核4.14版本——mmc core(10)——mmc core主模块(6)mmc请求相关
  4. idea代码字体大小放大和缩小的快捷键设置
  5. 给ecmall添加购物满100免运费的功能
  6. ubuntu20.04 wps office 系统缺失字体问题解决!
  7. 点开/双击TIM没有反应
  8. python剔除最小值_从同一行cod的列表中删除最小值和最大值
  9. CIO40: 数字化中心运营管理
  10. 基于蒙哥马利实现大数模密的算法的硬件实现