在上星期公司要求要把APP的首頁頭部圖片改為輪播圖
要知道一個輪播圖實現(xiàn)的方式有太多太多了
正在猶豫的時候突然想到了項目中的跑馬燈的一個動畫效果
就想著是否可以利用動畫的方式來進行輪播圖

5_0d9dc79c10fdca56880a18c6be87b283.gif
試了一下
沒想到還真的可以
這就太帥了
還可以簡化代碼還可以節(jié)約內(nèi)存
不啰嗦了,上代碼
@property (weak ,nonatomic)UIImageView * imageBack;
@property(assign ,nonatomic)NSTimer * Time;
/**
創(chuàng)建UI
*/
-(void)createUI{
//背景圖片
UIImageView * imageBack = [[UIImageView alloc]initWithFrame:self.bounds];
_imageBack = imageBack;
imageBack.image = [UIImage imageNamed:@"背景首頁"];
[self addSubview:imageBack];
_Time = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
}
-(void)timerClick{
[self.imageBack sd_setImageWithURL:[NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/h%3D300/sign=b12ec0dd93510fb367197197e932c893/b999a9014c086e064a76b12f0f087bf40bd1cbfc.jpg"] placeholderImage:[UIImage imageNamed:@"加載中"]];
[self.imageBack.layer addAnimation:[self createTransitionAnimation] forKey:nil];
}
-(CATransition *)createTransitionAnimation
{
//切換之前添加動畫效果
//后面知識: Core Animation 核心動畫
//不要寫成: CATransaction
//創(chuàng)建CATransition動畫對象
CATransition *animation = [CATransition animation];
//設(shè)置動畫的類型:
animation.type = @"push";
//設(shè)置動畫的方向
animation.subtype = kCATransitionFromRight;
//設(shè)置動畫的持續(xù)時間
animation.duration = 0.5f;
//設(shè)置動畫速率(可變的)
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
//動畫添加到切換的過程中
return animation;
}
展示圖

QQ20181024-142547.gif