您的位置:首页 > 产品设计 > UI/UE

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

2015-06-16 18:40 666 查看
(1)UIImageView 的动画操作,来自定义循环播放动画(不建议使用,内存消耗大)

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

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

关键操作:

















[b]效果如下:[/b]





[b]ViewController.h[/b]

#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=0;
static NSUInteger indexOfImg = 0;
//每秒执行12次if内的语句;分别当s=5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60...
s++;
if (s % 5 == 0) {
UIImage *image=[self imagesFromGroups][indexOfImg];
_imgVAnimation.layer.contents=(id)image.CGImage; //更新图片

indexOfImg++;
if (indexOfImg == kImgCount) {
indexOfImg = 0;
}
}
}

#pragma mark - 不建议使用的方式
/**
*  使用图片视图的动画操作,来自定义循环播放动画(不建议使用,内存消耗大)
*/
- (void)layoutUINoSuggest {
_imgVAnimation = [[UIImageView alloc] initWithFrame:self.view.bounds];
_imgVAnimation.animationImages = [self imagesFromGroups]; //引用图片数组,导致一次性加载图片数组,内存消耗大
//设置动画持续时间(图片播放周期时间,而不是播放一张图片的时间);单位为秒;默认值为每秒30帧(每秒播放30张图片)
_imgVAnimation.animationDuration = 3;
//设置动画播放重复次数;默认值为0,表示无限循环
_imgVAnimation.animationRepeatCount = 0;
[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=0; i<kImgCount; i++) {
strImgName = [NSString stringWithFormat:(i<10 ? @"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=0; i<kImgCount; i++) {
strImgName = [NSString stringWithFormat:(i<10 ? @"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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: