制作效果:一组图片以gif的形式出现且 带渐入消失的效果
代码如下:
声明属性
@property (weak, nonatomic) IBOutlet UIImageView *welcomeImage;//欢迎界面ImageView
@property (nonatomic,strong) NSArray* imageArray;//图片数组
图片数组
#pragma mark--懒加载
-(NSArray*) imageArray{
if (!_imageArray) {
_imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"welcome01.jpg"], [UIImage imageNamed:@"welcome02.jpg"],[UIImage imageNamed:@"welcome03.jpg"],nil];
}
return _imageArray;
}
-(NSTimer*) timer{
if (!_timer) {
self.welcomeImage.image = [UIImage imageNamed:@"welcome01.jpg"];//初始图片
//启动定时器进行图片的循环播放
_timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(frameAnimation) userInfo:nil repeats:YES];
}
return _timer;
}
数据加载完毕 (由于需要在定时器中进行图片的更替,则给予一个ImageView的初始值最后一张图片,在定时器的回调方法中进行启动,进行图片的由123顺序展示)
pragma mark--视图加载完毕
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
//启动定时器
[self.timer setFireDate:[NSDate distantPast]];
}
定时器回调方法
#pragma mark--类似帧动画但额外渐入消失效果
-(void)frameAnimation{
//创建CATransition对象
CATransition *transition = [CATransition animation];
//设置运动类型
transition.type = kCATransitionFade;
//设置运动时间
transition.duration = 2.0f;
//设置运动速度
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//图片layer层添加动画
[self.welcomeImage.layer addAnimation:transition forKey:nil];
//获取此时的数组图片
UIImage *currentImage = self.welcomeImage.image;
//获取该图片在数组中的位置
NSUInteger index = [self.imageArray indexOfObject:currentImage];
//对于得到的位置数较数组长度取余 得到下一张图片的位置数
index = (index + 1) % [self.imageArray count];
//更改图片为下一张
self.welcomeImage.image = self.imageArray[index];
}
方法二 在定时器回调方法中替换方法一
UIImage *currentImage = self.imageView.image;
NSUInteger index = [self.imgs indexOfObject:currentImage];
index = (index + 1) % [self.imgs count];
[UIView transitionWithView:self.imageView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.imageView.image = self.imgs[index];
} completion:nil];
网友评论