//
// ViewController.m
// OcDemoTest
//
// Created by Mac on 2023/7/14.
//#import "ViewController.h"@interface ViewController ()
// label
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
// Use of undeclared identifier
// 全局@property (nonatomic,strong) NSArray *standImages;
@property (nonatomic,strong) NSArray *samallImages;
@property (nonatomic,strong) NSArray *bigimage;@end@implementation ViewController/***图片的两种加载方式1.imageNamed:该资源也不会被从内存中干掉放到Assets.xcassets的图片 默认有缓存使用 场景图片经常使用iimageWithcontentsOfFile指向它的指针被销毁,该资源会被内存中干掉放到项目中的图片没有缓存使用场景。不经常用,大批量图片*/- (void)viewDidLoad {[super viewDidLoad];// 1.加载所有站立的图片
// NSMutableArray<UIImage *> *standImages = [NSMutableArray array];
// for(int i = 0;i<10;i++){
// // 获取所有图片的名称
// NSString *imageName = [NSString stringWithFormat:@"stand_%d",i+1];
// // 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
// // 装入数组
// [standImages addObject:image];
// }self.standImages = [self loadAllImagesWithimagePrefix:@"stand" count:10];// NSMutableArray<UIImage *> *samallImages = [NSMutableArray array];
// for(int i = 0;i<39;i++){
// // 获取所有图片的名称
// NSString *imageName = [NSString stringWithFormat:@"xiaozhao3_%d",i+1];
// // 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
// // 装入数组
// [samallImages addObject:image];
// }self.samallImages = [self loadAllImagesWithimagePrefix:@"xiaozhao3" count:39];
// NSMutableArray<UIImage *> *bigimage = [NSMutableArray array];
// for(int i = 0;i<87;i++){
// // 获取所有图片的名称
// NSString *imageName = [NSString stringWithFormat:@"dazhao_%d",i+1];
// // 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
// // 装入数组
// [bigimage addObject:image];
// }
// self.bigimage = bigimage;self.bigimage = [self loadAllImagesWithimagePrefix:@"dazhao" count:87];// 进来就站立[self stand];}// 封装方法
// 加载所有图片
/**imagePrefix 名称count 数量*/
-(NSArray *)loadAllImagesWithimagePrefix:(NSString *)imagePrefix count:(int)count{NSMutableArray<UIImage *> *images = [NSMutableArray array];for(int i = 0;i<count;i++){// 获取所有图片的名称NSString *imageName = [NSString stringWithFormat:@"%@_%d",imagePrefix,i+1];// 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];UIImage *image = [UIImage imageWithContentsOfFile:imagePath];// 装入数组[images addObject:image];}return images;}
// 放招// 站立
- (IBAction)stand{// 加载所有图片
// NSMutableArray<UIImage *> *standImages = [NSMutableArray array];
// for(int i = 0;i<10;i++){
// // 获取所有图片的名称
// NSString *imageName = [NSString stringWithFormat:@"stand_%d",i+1];
// // 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
// // 装入数组
// [standImages addObject:image];
// }
// //设置动画图片
// self.imageView.animationImages = self.standImages;
// // 设置播放次数
// self.imageView.animationRepeatCount = 0;
// // 设置播放时长
// self.imageView.animationDuration = 0.5;
// // 播放
// [self.imageView startAnimating];//[self stand];[self playZhaoWithImage:self.standImages playcount:0 duration:0.5 isStand:YES];}// 小招
- (IBAction)smallZhao {// 加载所有图片
// NSMutableArray<UIImage *> *samallImages = [NSMutableArray array];
// for(int i = 0;i<39;i++){
// // 获取所有图片的名称
// NSString *imageName = [NSString stringWithFormat:@"xiaozhao3_%d",i+1];
// // 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
// // 装入数组
// [samallImages addObject:image];
// }// 设置动画图片
// self.imageView.animationImages = self.samallImages;
// // 设置播放次数
// self.imageView.animationRepeatCount = 1;
// // 设置播放时长
// self.imageView.animationDuration = 1;
// // 播放
// [self.imageView startAnimating];// 释放后站立 延迟// Selector方法// Object 参数// afterDelay 时间// [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];[self playZhaoWithImage:self.samallImages playcount:1 duration:1.5 isStand:NO];}// 大招
- (IBAction)bigZhao{// 加载所有图片
// NSMutableArray<UIImage *> *bigimage = [NSMutableArray array];
// for(int i = 0;i<87;i++){
// // 获取所有图片的名称
// NSString *imageName = [NSString stringWithFormat:@"dazhao_%d",i+1];
// // 创建UIImage
// UIImage *image = [UIImage imageNamed:imageName];
// // 装入数组
// [bigimage addObject:image];
// }// 设置动画图片
// self.imageView.animationImages = self.bigimage;
// // 设置播放次数
// self.imageView.animationRepeatCount = 1;
// // 设置播放时长
// self.imageView.animationDuration = 2.0;
// // 播放
// [self.imageView startAnimating];// [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];[self playZhaoWithImage:self.bigimage playcount:1 duration:2.0 isStand:NO];
}
// 游戏结束
- (IBAction)gameOver:(id)sender {self.standImages = nil;self.samallImages = nil;self.bigimage = nil;self.imageView.animationImages = nil;}-(void)playZhaoWithImage:(NSArray *)images playcount:(NSInteger)playcount duration:(double)duration isStand:(BOOL)isStand{self.imageView.animationImages = images;// 设置播放次数self.imageView.animationRepeatCount = playcount;// 设置播放时长self.imageView.animationDuration = duration;// 播放[self.imageView startAnimating];//站立if(!isStand){[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];}}@end