UIImageView的animationImages,不给力啊


由于我们的项目需要,我正在写一个播放序列帧的程序,第一时间想起来的就是apple中UIImageView的属性animationImages,将一系列帧添加到一个数组里面,然后设置animation一系列属性,如动画时间,动画重复次数,还是看代码吧,直观

Source code    
imagesArray = [NSArray arrayWithObjects:
            [UIImage imageNamed:@"image1.png"],
            [UIImage imageNamed:@"image2.png"],
            [UIImage imageNamed:@"image3.png"],
            [UIImage imageNamed:@"image4.png"],
            [UIImage imageNamed:@"image5.png"],nil];
 
animationImageView = [UIImageView alloc];
[animationImageView initWithFrame:CGRectMake(0, 0, 131, 125)];
animationImageView.animationImages = imagesArray;//将序列帧数组赋给UIImageView的animationImages属性
animationImageView.animationDuration = 0.25;//设置动画时间
animationImageView.animationRepeatCount = 0;//设置动画次数 0 表示无限
[animationImageView startAnimating];//开始播放动画
 
[self addSubview:animationImageView];

设置动画的代码结构就是这样,如果图片少的话也许这种方式是最快速最容易达到目的的,那么你会问图片很多不行吗?,恩,根据目前我做的实验,图片很多的话这种方式程序必须会蹦,随后我会提到我们现在的实现方式,而且动画不能够实现暂停,只有停止,项目中要求序列帧播放的时候当手轻触(touch)播放暂停,松开后继续播放 ,横扫(swipe)播放加速,这一系列的需求表明了用animationImages这种方式实现已经不太现实.

之后我们考虑了自己去实现UIImageView的animation效果,题目之所以说他不给力,因为UIImageView的animation不会边用边释放(当然这点仅是我自己的拙见),那就导致了如果图片很多,animation直接崩掉根本用不了,我们实现的原理就是用NSTimer去实现apple的UIImageView animation的效果,其实apple应该也是用NSTimer去实现吧(猜的),用NSTimer每隔一个时间戳去设置一次image,代码如下

Source code    
NSTimer *myAnimatedTimer = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector(setNextImage) userInfo:nil repeats:YES];
void setNextImage
{
	myAnimatedView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%i.png",nextImage]];
}

下面是自定义类去实现类似UIImageView animation代码结构

Source code    
//
//  SXAnimationImageView.h
//  MyAnimation
//
//  Created by  on 12-2-12.
//  Copyright (c) 2012年 CocoaChina. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
#define LEFT -1
#define RIGHT 1
 
typedef enum {
    SXAnimationImageViewStatePlaying,
    SXAnimationImageViewStateStop,
    SXAnimationImageViewStateStoped,
    // add State
}SXAnimationImageViewState;
 
typedef enum {
    SXAnimationImageViewPre,
    SXAnimationImageViewNext,
}SXAnimationImageViewPreOrNext;
 
@interface SXAnimationImageView : UIView
{
    UIImageView *                   _imageView;
 
    NSTimer *                       _timer;
    NSUInteger                      _currentIndex;
    NSUInteger                      _jump;
 
    SXAnimationImageViewPreOrNext   _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

实现文件部分代码:

Source code    
@interface SXAnimationImageView (SX)
- (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;
 
@end


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM