- myImages
= [NSArray arrayWithObjects: -
[UIImage imageNamed:@"mario_1.png"], -
[UIImage imageNamed:@"mario_2.png"], -
[UIImage imageNamed:@"mario_3.png"], -
[UIImage imageNamed:@"mario_4.png"], -
[UIImage imageNamed:@"mario_5.png"],nil]; -
-
myAnimatedView = [UIImageView alloc]; -
[myAnimatedView initWithFrame:CGRectMake(0, 0, 131, 125)]; -
myAnimatedView.animationImages = myImages;//将序列帧数组赋给UIImageView的animationImages属性 -
myAnimatedView.animationDuration = 0.25;//设置动画时间 -
myAnimatedView.animationRepeatCount = 0;//设置动画次数 0 表示无限 -
[myAnimatedView startAnimating];//开始播放动画 -
-
[self addSubview:myAnimatedView];
- NSTimer
*myAnimatedTimer = [NSTimer scheduledTimerWithTimeIn terval:0.04 target:self selector:@selector(setNextImage) userInfo:nil repeats:YES]; - void
setNextImage - {
-
myAnimatedView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%i.png",nextImage]]; - }
下面是部分代码结构
- //
- //
SXAnimationImageView.h - //
- //
Created by ShinSuo on 12-2-12. - //
Copyright (c) 2012年 CocoaChina. All rights reserved. - //
-
- #import
-
- #define
LEFT -1 - #define
RIGHT 1 -
- typedef
enum { -
SXAnimationImageViewStat ePlaying, -
SXAnimationImageViewStat eStop, -
SXAnimationImageViewStat eStoped, -
// add State - }SXAnimationImageViewStat
e; -
- typedef
enum { -
SXAnimationImageViewPre, -
SXAnimationImageViewNext , - }SXAnimationImageViewPreO
rNext; -
- @interface
SXAnimationImageView : UIView - {
-
UIImageView * _imageView; -
-
NSTimer * _timer; -
NSUInteger _currentIndex; -
NSUInteger _jump; -
-
SXAnimationImageViewPreO rNext _preOrNext; -
UIPanGestureRecognizer * _panGesture; -
// add Gesture - }
-
- @property
NSTimeInterval animationDuration; - @property
NSUInteger animationRepeatCount; - @property
NSUInteger toIndex; - @property
(strong,nonatomic) NSMutableArray * animationImages; -
-
-
- -
(void)animationTo:(NSUInteger)index_; - -
(void)setCurrentImage; -
- -
(void)startAnimating; - -
(void)stopAnimating; - -
(BOOL)isAnimating; -
- @end
部分实现文件代码
- //
- //
SXAnimationImageView.m - //
- //
Created by ShinSuo on 12-2-12. - //
Copyright (c) 2012年 CocoaChina. All rights reserved. - //
-
- #import
"SXAnimationImageView.h" -
- @interface
SXAnimationImageView () - -
(void)preCache;// prepare read image - -
(void)animationFrom:(NSUInteger)fromIndex_ To:(NSUInteger)toIndex_; - -
(void)waitForStop:(NSNotification *)notification; - -
(void)stateChanged:(NSNotification *)notification; - @end
-
- @implementation
SXAnimationImageView -
- @synthesize
animationDuration = _animationDuration; - @synthesize
animationRepeatCount = _animationRepeatCount; - @synthesize
toIndex = _toIndex; - @synthesize
animationImages = _animationImages; -
- -
(void)setAnimationImages:(NSMutableArray *)animationImages_ - {
-
if (_animationImages == nil) { -
_animationImages = [[NSMutableArray alloc] init]; -
} -
_animationImages = animationImages_; -
-
[self preCache]; - }
-
- -
(void)preCache - {
-
_imageView.animationImages = _animationImages; -
_imageView.animationDuration = 0.4; -
_imageView.animationRepeatCount = 1; -
[_imageView startAnimating]; -
[_imageView performSelector:@selector(stopAnimating) withObject:nil afterDelay:0.41]; - }
-
-
-
- @end
这个实现文件中有一个投机取巧的地方,有些同学应该注意到了,就是preCache,这里这种方法也仅限于图片比较少的时候,如果图片很多的话是不可取的照样会崩的,用过UIImageViewanimationImages属性的应该知道,第一次加载的时候会很慢,animation完成一次周期之后就很流畅了,那么我们这里,在程序启动的时候可以先将对SXAnimationImageView的animationImages赋值(会调用preCache),这样当视图没有加载的时候其实已经完成一次动画了,只是你没有看到,等程序启动起来后视图显示在你眼前的时候动画已经很流畅了