在做应用的时候会碰到灯火闪烁或者图片渐变的过程,这个时候可以使用UIImageView自带的一些方法和属性完成多张图片连续显示,即可完成一个小动画
常用UIImageView自带的三个属性
1. animationImages 表示需要显示的图片的内容 , 传入一个数组
@property(nonatomic,copy) NSArray *animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
2. animationDuration 表示每张图片间隔的时间 默认为30 fps,即每秒30帧@property(nonatomic)NSTimeInterval animationDuration; // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
3. animationRepeatCount 表示动画重复的次数
@property(nonatomic)NSInteger animationRepeatCount; // 0 means infinite (default is 0)
4. -(void)startAnimating表示开始动画
比如要实现一个闪烁效果,可以把两张图片放在一个数组中,并且并且设置切换的频率
UIImage *image1 = [UIImageimageNamed:@"lucky_entry_light0"];
UIImage *image2 = [UIImageimageNamed:@"lucky_entry_light1"];
NSArray *array = @[image1, image2];
_lightImage.animationImages = array;
_lightImage.animationRepeatCount = 30;
_lightImage.animationDuration =0.2;
[_lightImagestartAnimating];
在这个栗子中,使用了两张图片完成灯光闪烁效果,闪烁的频率是5fps,重复次数为30