Obj-C大量图片构成动画的实现(二)

前几天写了关于Obj-C大量图片构成动画的实现。实际项目中的要求进一步提高,要求大量的640*960的图片构成动画。这下又郁闷了,之前的方案在Load大量如此图片时各种Crash啊。

由于良久,决定自己写一个UIAnimationView来手动实现图片动画的功能。

原理非常简单,Timer控制UIImageView的图片变换。同时,类的内部设两个Array,用来存储动画需要的图片集合。

鉴于本程序的需求,设计一个DefaultArray用于播放默认的动画,设一个ImageArray用于播放需要的动画。

@interface UIAnimationView : UIView {
UIImageView * _imageView;
NSArray * _imageArray;
NSTimer * _animateTimer;
BOOL _isStartAnimating;
int _curNumber;
NSString * _fileType;
NSArray * _imageDefaultArray;
int _curDefaultArray;
}

然后写个类的初始化函数,非常简单。

- (id) initWithFrame:(CGRect)frame withDefaultImageArray:(NSArray *)defaultArray{
self = [super initWithFrame:frame];
if (self) {
_curNumber = 0;
_curDefaultArray = 0;
_imageView = [[UIImageView alloc] initWithFrame:frame];
_animateTimer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(runAnimation:) userInfo:nil repeats:YES];
_imageDefaultArray = defaultArray;
}
[self addSubview:_imageView];
return self;
}

最后,加入该类的动画执行函数。

- (void)runAnimation:(NSTimer *)timer{
if (_imageArray != nil) {
if (_curNumber < [_imageArray count]) {
_imageView.image = nil;
_imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[_imageArray objectAtIndex:_curNumber] ofType:_fileType]];
_curNumber ++;
}else{
_imageArray = nil;
_curNumber = 0;
}
}else{
if (_curDefaultArray < [_imageDefaultArray count]) {
_imageView.image = nil;
_imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[_imageDefaultArray objectAtIndex:_curDefaultArray] ofType:@"jpg"]];
_curDefaultArray ++;
}else{
_curDefaultArray = 0;
}
}
}





posted @ 2012-04-02 01:46  Andy Wang  阅读(488)  评论(0编辑  收藏  举报