(1)UIImageView 的动画操作,来自定义循环播放动画(不建议使用,内存消耗大)

(2)CADisplayLink 是一个计时器,但是同 NSTimer 不同的是,CADisplayLink 的刷新周期同屏幕完全一致。

例如在 iOS 中屏幕刷新周期是60次/秒,CADisplayLink 刷新周期同屏幕刷新一致也是60次/秒,这样一来使用它完成的逐帧动画(又称为“时钟动画”)完全感觉不到动画的停滞情况。

关键操作:

效果如下:

ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController
@property (strong, nonatomic) UIImageView *imgVAnimation;
@property (strong, nonatomic) CADisplayLink *displayLink; @end

ViewController.m

 #import "ViewController.h"

 @interface ViewController ()
- (void)layoutUI;
- (void)changeImage;
- (void)layoutUINoSuggest;
- (NSArray *)imagesFromGroups;
- (NSArray *)imagesFromFolderReferences;
@end @implementation ViewController
#define kImgCount 29 - (void)viewDidLoad {
[super viewDidLoad]; //[self layoutUINoSuggest]; [self layoutUI];
} - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; //开始动画;对应使用[self layoutUINoSuggest]的情况
//[_imgVAnimation startAnimating]; //实例化时钟对象
_displayLink=[CADisplayLink displayLinkWithTarget:self selector:@selector(changeImage)];
//添加时钟对象实例到主运行循环
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
} - (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; //停止动画;对应使用[self layoutUINoSuggest]的情况
//[_imgVAnimation stopAnimating]; //从主运行循环移除时钟对象实例
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - 推荐使用的方式
/**
* 使用CADisplayLink,来自定义循环播放动画(推荐使用,内存消耗小)
* CADisplayLink是一个计时器,但是同NSTimer不同的是,CADisplayLink的刷新周期同屏幕完全一致。例如在iOS中屏幕刷新周期是60次/秒,CADisplayLink刷新周期同屏幕刷新一致也是60次/秒,这样一来使用它完成的逐帧动画(又称为“时钟动画”)完全感觉不到动画的停滞情况
*/
- (void)layoutUI {
_imgVAnimation = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_imgVAnimation];
} - (void)changeImage {
//定义一个变量记录执行次数
static NSUInteger s=;
static NSUInteger indexOfImg = ;
//每秒执行12次if内的语句;分别当s=5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60...
s++;
if (s % == ) {
UIImage *image=[self imagesFromGroups][indexOfImg];
_imgVAnimation.layer.contents=(id)image.CGImage; //更新图片 indexOfImg++;
if (indexOfImg == kImgCount) {
indexOfImg = ;
}
}
} #pragma mark - 不建议使用的方式
/**
* 使用图片视图的动画操作,来自定义循环播放动画(不建议使用,内存消耗大)
*/
- (void)layoutUINoSuggest {
_imgVAnimation = [[UIImageView alloc] initWithFrame:self.view.bounds];
_imgVAnimation.animationImages = [self imagesFromGroups]; //引用图片数组,导致一次性加载图片数组,内存消耗大
//设置动画持续时间(图片播放周期时间,而不是播放一张图片的时间);单位为秒;默认值为每秒30帧(每秒播放30张图片)
_imgVAnimation.animationDuration = ;
//设置动画播放重复次数;默认值为0,表示无限循环
_imgVAnimation.animationRepeatCount = ;
[self.view addSubview:_imgVAnimation];
} #pragma mark - 读取图片文件数组操作
/**
* 获取来自分组(黄色文件夹)的图片数组;图片文件路径不需要包含文件夹
* 使用右键“Add Files to...”->“Added folders” : “Create groups”,生成分组(黄色文件夹)
*
* @return 来自分组(黄色文件夹)的图片数组
*/
- (NSArray *)imagesFromGroups {
NSMutableArray *mArrImgForAnimation = [[NSMutableArray alloc] initWithCapacity:kImgCount];
NSString *strImgName;
for (NSUInteger i=; i<kImgCount; i++) {
strImgName = [NSString stringWithFormat:(i< ? @"Happy000%lu" : @"Happy00%lu")
, (unsigned long)i];
//[mArrImgForAnimation addObject:[UIImage imageNamed:strImgName]]; //[UIImage imageNamed:strImgName]会缓存图片,这里图片多,占内存过大,不建议用 //读取方式一(推荐使用):
NSString *path = [[NSBundle mainBundle] pathForResource:strImgName ofType:@"jpg"];
//NSString *path = [[NSBundle mainBundle] pathForResource:strImgName ofType:nil]; //这种方式的话,strImgName的格式就为“xx.jpg” //读取方式二:
//NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:strImgName]; //为数组mArrImgForAnimation添加数组元素
[mArrImgForAnimation addObject:[UIImage imageWithContentsOfFile:path]]; //虽然没缓存图片,但也可能存在内存泄露问题
}
return mArrImgForAnimation;
} /**
* 获取来自文件夹引用(蓝色文件夹)的图片数组;图片文件路径需要包含文件夹
* 使用右键“Add Files to...”->“Added folders” : “Create folder references”,生成文件夹引用(蓝色文件夹)
*
* @return 来自文件夹引用(蓝色文件夹)的图片数组
*/
- (NSArray *)imagesFromFolderReferences {
NSMutableArray *mArrImgForAnimation = [[NSMutableArray alloc] initWithCapacity:kImgCount];
NSString *strImgName;
for (NSUInteger i=; i<kImgCount; i++) {
strImgName = [NSString stringWithFormat:(i< ? @"Happy000%lu" : @"Happy00%lu")
, (unsigned long)i]; //读取方式一(推荐使用):
NSString *path = [[NSBundle mainBundle] pathForResource:strImgName ofType:@"jpg" inDirectory:@"TomCat"];
//NSString *path = [[NSBundle mainBundle] pathForResource:strImgName ofType:nil inDirectory:@"TomCat"]; //这种方式的话,strImgName的格式就为“xx.jpg” //读取方式二:
//NSString *bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TomCat"];
//NSString *path = [bundlePath stringByAppendingPathComponent:strImgName]; //为数组mArrImgForAnimation添加数组元素
[mArrImgForAnimation addObject:[UIImage imageWithContentsOfFile:path]]; //虽然没缓存图片,但也可能存在内存泄露问题
}
return mArrImgForAnimation;
} @end

156 UIImageView 和 CADisplayLink 实现 Tom 汤姆猫动画效果的区别(扩展知识:分组(黄色文件夹)和文件夹引用(蓝色文件夹)区别)的更多相关文章

  1. iOS- 利用UIImageView自己整了个不会说话的汤姆猫

    1.实现思路 先说说我实现它的主要思路,很简单,主要利用UIImageView连续动画播放,和按钮的点击事件,就可以完成了这么一个简单的不会说话的汤姆猫. 2.实现细节 2.1.加载本地字典里保存的本 ...

  2. [iOS基础控件 - 3.4] 汤姆猫

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  3. [UI基础][不会说话的汤姆猫]

    会说话的汤姆猫这个APP层级风靡一时,其UI部分就是利用了序列动画的技术, 接下来 我们用汤姆猫来演示怎么制作序列动画. [要求]: 1.学会使用序列动画的方法 2.学会分析动画播放中内存占用高的问题 ...

  4. 声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码

    上周看到一个变调算法,挺有意思的,原本计划尝试用来润色TTS合成效果的. 实测感觉还需要进一步改进,待有空再思考改进方案. 算法细节原文,移步链接: http://blogs.zynaptiq.com ...

  5. iOS_5_汤姆猫

    终于效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...

  6. 使用UISegementControl实现简易汤姆猫程序

    // // TomViewController.m #import "TomViewController.h" #import <AVFoundation/AVFoundat ...

  7. IOS 汤姆猫核心代码

    // // MJViewController.m // 03-Tom // // Created by apple on 13-11-24. // Copyright (c) 2013年 itcast ...

  8. 【Web网站服务器开发】apache和tomcat 阿帕奇和汤姆猫

    经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料,总 ...

  9. Web核心之tomcat汤姆猫

    web相关概念 1. 软件架构 1. C/S:客户端/服务器端 2. B/S:浏览器/服务器端 2. 资源分类 1. 静态资源:所有用户访问后,得到的结果都是一样的,称为静态资源.静态资源可以直接被浏 ...

随机推荐

  1. python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)

    pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...

  2. python. pandas(series,dataframe,index) method test

    python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...

  3. Android RecyclerView网格布局

    一个简单的网格布局activity_main.xml <?xml version="1.0" encoding="utf-8"?> <andr ...

  4. jython 2.7.1 版本开发历史

    很多技术一直感兴趣,在工作中没有太多实践的机会,但可以持续关注.Jython最初是Python on JVM,所以最初的名称是JPython,后续由于基于JVM可以有更多可能性,名字改为Jython. ...

  5. Postgresql查询表的大小

    --数据库中单个表的大小(不包含索引) select pg_size_pretty(pg_relation_size('表名')); --查出所有表(包含索引)并排序 SELECT table_sch ...

  6. shell+钉钉机器人完成java程序中断后自启动和实时监控

    java实时程序在运行过程中偶尔出现异常信息中断的情况,通过shell脚本即可完成自启动. 以下为监控一个实时的java程序的shell脚本. 通过每10秒检查一次java程序的进程,来判断程序是否处 ...

  7. 【linux】——ubuntu12.04 下安装wine和wine乱码解决方案

    ————————安装最新ppa的wine———————— sudo add-apt-repository ppa:ubuntu-wine/ppa sudo apt-get update sudo ap ...

  8. 多变量频率统计——r

    例如有X1,X2,..,Xn个变量,我需要对每一个变量进行频次统计,如果一个一个求解的话非常麻烦,如table(X1), table(X2), ... ,table(Xn).有没有简单的语句一次性求解 ...

  9. 安卓程序代写 网上程序代写[原]Android开发技巧--ListView

    1. ListView中元素的排序 ListView中的元素排序, 即将数据源排序即可; 给集合排序的方法 : 调用Collections的sort(list, Comparator)方法, 该方法需 ...

  10. mac配置--ant

    每次在新的电脑安装开发工具总是免不了下载各种软件和配置环境,本文针对mac下安装ant小结一下. 安装ant的方法很多,最直接的可以到apache-ant官网http://ant.apache.org ...