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

IOS1.7-UIImageView的帧动画相关属性和方法 (示例汤姆猫)

2015-01-11 18:51 591 查看
UIImageView的帧动画相关属性和方法 (示例汤姆猫)                                                                            

一:下面是UIImageView中关于帧动画的一些属性和方法

@property(nonatomic,copy) NSArray *animationImages;
需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)

@property(nonatomic) NSTimeInterval animationDuration;
帧动画的持续时间

@property(nonatomic) NSInteger animationRepeatCount;
帧动画的执行次数(默认是无限循环)

- (void)startAnimating;
开始执行帧动画

- (void)stopAnimating;
停止执行帧动画

- (BOOL)isAnimating;
是否正在执行帧动画

二:汤姆猫示例

/** 做动画*/
-(void)doAnimationCount:(int)count andImagesName:(NSString *)imageName
{
if (self.backImage.isAnimating) return ;
//创建数组存储图片
NSMutableArray *images = [NSMutableArray array];

for (int i=0; i<count; i++) {
//取得图片名字
NSString *str = [NSString stringWithFormat: @"%@_%02d.jpg",imageName, i];
//NSLog(@"%@",image);
//获得每张图片
//UIImage *image  = [UIImage imageNamed:str];//重点!!!这个方法有缓存,缓存还会叠加,所以不用这个,下面这种方法没有缓存

//找到资源总文件夹
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:str ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
//添加到数组
[images addObject:image];
}
//获取图片(图片播放完并没有被释放) 只要animationImages这个属性在,图片就在,除非把属性清空
self.backImage.animationImages = images;
//设置播放时间
self.backImage.animationDuration = images.count *0.07 ;
//执行一次
self.backImage.animationRepeatCount = 1;

//开始动画
[self.backImage startAnimating];
//多长时间之后释放内存,也就是把animationImages这个属性给清空,因为只要animationImages这个属性在,图片就在,除非把数组清空
CGFloat delay =self.backImage.animationDuration + 1;
[self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
//上面这句加上下面的方法可以简化成下面,注意:下面这句才是精华!!!!!!!!!
//[self performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];

//[self performSelector:@selector(selector) withObject:<#(id)#> withObject:<#(id)#>];
}
/**
*  清缓存
*/
-(void)clearCache
{
self.backImage.animationImages = nil;
//[self.backImage setAnimationImages:nil];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIImageView 帧动画