ios10--拳皇动画

简介:
复制代码
 
  
/**
    图片的两种加载方式:
    1> imageNamed:
      a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉,
      b. 放到Assets.xcassets的图片,默认就有缓存,
      c. 图片经常被使用,用这种方式加载,
 
    2> imageWithContentsOfFile:
      a. 指向它的指针被销毁,该资源会被从内存中干掉,
      b. 放到项目中的图片就不带缓存,
      c. 不经常用,大批量的图片,使用这种方式,

 */
 
  
/**
 *  游戏结束
 */
- (IBAction)gameOver {  //没有强指针,就把图片释放了,即使是图片数组,只要把图片数组的首地址置位nil,数组就释放了,所以只要首地址置位nil就可以
    self.standImages = nil;
    self.smallImages = nil;
    self.bigImages = nil;
    
    self.imageView.animationImages = nil;
}






//
// ViewController.m // 07-拳皇动画(加载图片) // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; /** 站立 */ @property (nonatomic, strong) NSArray *standImages; //不是故事板上的东西,没有强引用指向,所以这里用strong, /** 小招 */ @property (nonatomic, strong) NSArray *smallImages; /** 大招 */ @property (nonatomic, strong) NSArray *bigImages; @end @implementation ViewController // 初始化一些数据 - (void)viewDidLoad { [super viewDidLoad]; // 1.加载所有的站立图片 self.standImages = [self loadAllImagesWithimagePrefix:@"stand" count:10]; // 2.加载所有的小招图片 self.smallImages = [self loadAllImagesWithimagePrefix:@"xiaozhao3" count:39]; // 3.加载所有的大招图片 self.bigImages = [self loadAllImagesWithimagePrefix:@"dazhao" count:87]; // 4.站立 [self stand]; } /** * 加载所有的图片 * * @param imagePrefix 名称前缀 * @param 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]; // 装入数组 [images addObject:image]; } return images; } /** * 站立 */ - (IBAction)stand { // 2.设置动画图片 self.imageView.animationImages = self.standImages; // 3.设置播放次数 self.imageView.animationRepeatCount = 0; // 4.设置播放的时长 self.imageView.animationDuration = 0.6; // 5.播放 [self.imageView startAnimating]; } /** * 小招 */ - (IBAction)smallZhao { // 2.设置动画图片 self.imageView.animationImages = self.smallImages; // 3.设置动画次数 self.imageView.animationRepeatCount = 1; // 4.设置播放时长 self.imageView.animationDuration = 1.5; // 5.播放 [self.imageView startAnimating]; // 6.站立(延迟),执行一个方法,传入参数,延迟多长时间, // Selector 方法 // Object 参数 // afterDelay 时间 // NSSelectorFromString(NSString * _Nonnull aSelectorName) 效果一样 [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration]; } /** * 大招 */ - (IBAction)bigZhao { // 2.设置动画图片 self.imageView.animationImages = self.bigImages; // 3.设置动画次数 self.imageView.animationRepeatCount = 1; // 4.设置播放时长 self.imageView.animationDuration = 2.5; // 5.播放 [self.imageView startAnimating]; // 6.站立 [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration]; } @end
复制代码
复制代码
//
//  ViewController.m
//  07-拳皇动画(加载图片)
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

/** 站立 */
@property (nonatomic, strong) NSArray *standImages;//不是故事板上的东西,没有强引用指向,所以这里用strong,
/** 小招 */
@property (nonatomic, strong) NSArray *smallImages;
/** 大招 */
@property (nonatomic, strong) NSArray *bigImages;

@end

@implementation ViewController

/**
    图片的两种加载方式:
    1> imageNamed:
      a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉,
      b. 放到Assets.xcassets的图片,默认就有缓存,
      c. 图片经常被使用,用这种方式加载,
 
    2> imageWithContentsOfFile:
      a. 指向它的指针被销毁,该资源会被从内存中干掉,
      b. 放到项目中的图片就不带缓存,
      c. 不经常用,大批量的图片,使用这种方式,

 */

// 初始化一些数据
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.加载所有的站立图片
    self.standImages = [self loadAllImagesWithimagePrefix:@"stand" count:10];
    
    // 2.加载所有的小招图片
    self.smallImages = [self loadAllImagesWithimagePrefix:@"xiaozhao3" count:39]; //不需要写外层文件夹的名字,只需要写图片文件的名字即可。
    
    // 3.加载所有的大招图片
    self.bigImages = [self loadAllImagesWithimagePrefix:@"dazhao" count:87];
    
    // 4.站立
    [self stand];
}

/**
 *  加载所有的图片
 *
 *  @param imagePrefix 名称前缀
 *  @param 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 {
    /*
    // 2.设置动画图片
    self.imageView.animationImages = self.standImages;
    
    // 3.设置播放次数
    self.imageView.animationRepeatCount = 0;
    
    // 4.设置播放的时长
    self.imageView.animationDuration = 0.6;
    
    // 5.播放
    [self.imageView startAnimating];
    */
    [self palyZhaoWithImages:self.standImages count:0 duration:0.6 isStand:YES];
}

/**
 *  小招
 */
- (IBAction)smallZhao {
    /*
    // 2.设置动画图片
    self.imageView.animationImages = self.smallImages;
    
    // 3.设置动画次数
    self.imageView.animationRepeatCount = 1;
    
    // 4.设置播放时长
    self.imageView.animationDuration = 1.5;
    
    // 5.播放
    [self.imageView startAnimating];
    
    // 6.站立(延迟)
    [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
     */
    [self palyZhaoWithImages:self.smallImages count:1 duration:1.5 isStand:NO];
}

/**
 *  大招
 */
- (IBAction)bigZhao {
    /*
    // 2.设置动画图片
    self.imageView.animationImages = self.bigImages;
    
    // 3.设置动画次数
    self.imageView.animationRepeatCount = 1;
    
    // 4.设置播放时长
    self.imageView.animationDuration = 2.5;
    
    // 5.播放
    [self.imageView startAnimating];
    
    // 6.站立
    [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
    */
    [self palyZhaoWithImages:self.bigImages count:1 duration:2.5 isStand:NO];
}

/**
 *  游戏结束
 */
- (IBAction)gameOver {  //没有强指针,就把图片释放了,即使是图片数组,只要把图片数组的首地址置位nil,数组就释放了,所以只要首地址置位nil就可以
    self.standImages = nil;
    self.smallImages = nil;
    self.bigImages = nil;
    
    self.imageView.animationImages = nil;
}


/**
 *  放招
 *
 *  @param images   图片数组
 *  @param count    播放次数
 *  @param duration 播放时间
 *  @param isStand  是否站立
 */
- (void)palyZhaoWithImages:(NSArray *)images count: (NSInteger)count duration:(NSTimeInterval)duration isStand:(BOOL)isStand{
    // 1.设置动画图片
    self.imageView.animationImages = images;
    
    // 2.设置动画次数
    self.imageView.animationRepeatCount = count;
    
    // 3.设置播放时长
    self.imageView.animationDuration = duration;
    
    // 4.播放
    [self.imageView startAnimating];
    
    // 5.站立
    if (!isStand) {
     [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
    }
}

@end
复制代码

/**
    图片的两种加载方式:
    1> imageNamed:
      a. 就算指向它的指针被销毁,该资源也不会被从内存中干掉,
      b. 放到Assets.xcassets的图片,默认就有缓存,
      c. 图片经常被使用,用这种方式加载,
 
    2> imageWithContentsOfFile:
      a. 指向它的指针被销毁,该资源会被从内存中干掉,
      b. 放到项目中的图片就不带缓存,
      c. 不经常用,大批量的图片,使用这种方式,

 */
/**
 *  游戏结束
 */
- (IBAction)gameOver {  //没有强指针,就把图片释放了,即使是图片数组,只要把图片数组的首地址置位nil,数组就释放了,所以只要首地址置位nil就可以
    self.standImages = nil;
    self.smallImages = nil;
    self.bigImages = nil;
    
    self.imageView.animationImages = nil;
}



相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
93 2
|
3月前
|
API 开发工具 iOS开发
iOS 开发高效率工具包:10 大必备工具
iOS 开发高效率工具包:10 大必备工具
48 1
|
3月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
54 1
|
8天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
12 0
|
23天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
198 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4