一、UIimageView类分析
.h文件一共44行,按功能份,分为展示图片和图片动画两部分.
图片动画部分:
一 存放图片的数组
@property(nonatomic,copy)NSArray *animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
@property(nonatomic,copy)NSArray *highlightedAnimationImagesNS_AVAILABLE_IOS(3_0); // The array must contain UIImages. Setting hides the single image. default is nil
二 动画间隔 1秒30帧
@property(nonatomic)NSTimeInterval animationDuration; // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
三 动画重复次数
@property(nonatomic)NSInteger animationRepeatCount; // 0 means infinite (default is 0)
// When tintColor is non-nil, any template images set on the image view will be colorized with that color.
// The tintColor is inherited through the superview hierarchy. See UIView for more information.
(nonatomic,retain)UIColor *tintColorNS_AVAILABLE_IOS(7_0);
五 开始动画
- (void)startAnimating;
六 停止动画
- (void)stopAnimating;
七、正在动画吗,防止一些效果上的bug
- (BOOL)isAnimating;
二、UIimageView中动画实现的原理
UIImageView自带的这个动画,使用的是逐帧动画
UIimageIView的动画的原理其实和GIF是一样的,也可以叫GIF动画。。。
动画就是通过以一种连续贴图的方式快速播放来实现的。
什么是逐帧动画
逐帧动画也称为帧动画,这是一种常见的动画形式,它的主要特点是每一帧都需要提供一张图片,并将组成动画所需的一系列图片分别放在不同的帧当中。当播放动画时,是一帧一帧顺序播放的,这样通过一帧一帧显示动画的图像序列来实现动画效果。由于所有的图片都是人工提供的,所以逐帧动画具有非常大的灵活性,几乎可以表现任何想表现的内容。但同时由于每一帧图片都需要我们动手操作产生,所以制作起来比较麻烦。
拿一个描述一株向日葵从苗芽状态成长到绽放花朵的过程动画来说,该向日葵由小变大,因此构成该动画的每一张图片都是不一样的,如下图:
这样顺序播放这组图片就是实现所需要的动画了。
三、使用技巧
1、图片名字的for循环:%0nd,n位数字,不够补0
2、如果正在动画:则不再执行动画的代码
// 如果正在动画,直接退出
if ([self.tomisAnimating])return;
不再执行Animation。
3、图片加载方式的区别,与内存的管理
图片存放的位置:
1、main bundle 不常使用的,大的
2、Images.xcassets 经常使用,小的
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using
imageWithContentsOfFile:
. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
方式一:有缓存(图片所占用的内存会一直停留在程序中)释放由系统负责
+ (UIImage *)imageNamed:(NSString
name是图片的文件名
方式二:无缓存(图片所占用的内存会在一些特定操作后被清除)
+ (UIImage *)imageWithContentsOfFile:(NSString
- (id)initWithContentsOfFile:(NSString
path是图片的全路径
清除图片内存
[self.tomperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tom.animationDuration];