随机轮流翻转动画
要写一个随机的轮流翻转的动画,于是参照网上的修修改改,记录一下
//frameView父视图 images图片集合
//轮流翻转
/**
* 翻转动画
*/
- (void)layerRotation:(UIView*)frameView list:(NSArray*)images {
//随机数
NSInteger index = [ETUtilsToolgetRandomNumber:0to:images.count-1];
WEAKSELF;
for(inti=0; i < images.count; i++) {
if(i == index) {
UIImageView* imageV = [frameViewviewWithTag:100+ i];
[self addLayerRotationWithImageV:imageV];
}
}
///第一次切换图片
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0* NSEC_PER_SEC)),dispatch_get_main_queue(),^{
for(inti =0; i < images.count; i++) {
if(i == index) {
UIImageView* imageV = [frameViewviewWithTag:100+ i];
[weakSelf changeImg:imageV str:images[i]];
}
}
});
///循环动画
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(5.0* NSEC_PER_SEC)),dispatch_get_main_queue(),^{
[weakSelf layerRotation:frameView list:images];
});
}
- (void)changeImg:(UIImageView*)imageV str:(NSString*)str
{
if([imageV.image isEqual:[UIImage imageNamed:@"Classify_icon"]]) {
[imageVsd_setImageWithURL:[NSURL URLWithString:str]];
}else{
imageV.image= [UIImage imageNamed:@"Classify_icon"];
}
}
- (void)addLayerRotationWithImageV:(UIImageView *)imageV {
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animation];
// 旋转角度, 其中的value表示图像旋转的最终位置
keyAnimation.values= [NSArrayarrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,1,0)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation((M_PI/2), 0,1,0)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,1,0)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,1,0)],
nil];
keyAnimation.keyTimes = @[@(0),@(0.2),@(0.4),@(1.0)];//每一个的取值范围是0-1
keyAnimation.duration=5.0;
keyAnimation.repeatCount=1;
[imageV.layeraddAnimation:keyAnimationforKey:@"transform"];
}
//参照:https://www.jianshu.com/p/12e973669a68