给UIImageView设置一组图片动画,每张图片停留时间不一祥
来源:互联网 发布:纱裙布料淘宝 编辑:程序博客网 时间:2024/04/30 03:31
给UIView 写个分类
//UIView+KeyFrameAnimation.h#import <UIKit/UIKit.h>@interface UIView (KeyFrameAnimation)- (void)addAnimationWithImages:(NSArray *)animationImages times:(NSArray *)times repeatCount:(float)count animationFrame:(CGRect)frame;@end
// UIView+KeyFrameAnimation.m#import "UIView+KeyFrameAnimation.h"@implementation UIView (KeyFrameAnimation)- (void)addAnimationWithImages:(NSArray *)animationImages times:(NSArray *)times repeatCount:(float)count animationFrame:(CGRect)frame{ NSArray *sumArray = [self sumWithArray:times];//前几个的和; NSTimeInterval total = [[sumArray lastObject] doubleValue]; NSMutableArray *keyTimes = [NSMutableArray arrayWithObjects:@0.0f, nil]; for (NSNumber *time in sumArray) { NSTimeInterval timeFloat = [time floatValue] / total; [keyTimes addObject:[NSNumber numberWithDouble:timeFloat]]; } [keyTimes addObject:@1.0f]; //CAImage 数组 NSMutableArray *array = [NSMutableArray array]; for (UIImage *image in animationImages) { CGImageRef cgImage = [image CGImage]; [array addObject:(__bridge id _Nonnull)(cgImage)]; } CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; animation.calculationMode = kCAAnimationDiscrete; animation.keyPath = @"contents"; animation.duration = total; animation.delegate = self; animation.repeatCount = count; animation.values = array; animation.keyTimes = keyTimes;//每帧比例相对1的比例时间数组[0...1] CALayer *layer = [CALayer layer]; layer.frame = frame; [layer addAnimation:animation forKey:nil]; [self.layer addSublayer:layer];}-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ [[self.layer.sublayers lastObject] removeFromSuperlayer];}-(NSArray *)sumWithArray:(NSArray*)array{ NSMutableArray *mutableArray = [NSMutableArray array]; double sum = 0; for (int i = 0; i < array.count; i++) { sum += [((NSNumber *)array[i]) floatValue]; [mutableArray addObject:[NSNumber numberWithFloat:sum]]; } return mutableArray;}@end
调用
-(void)animation{ NSArray *imagePaths = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"animations"]; NSMutableArray *animationImages = [NSMutableArray array]; NSArray* times = @[@0.7, @0.4, @0.3, @0.5, @0.4, @0.4, @0.4, @0.8]; for (NSString *imageName in imagePaths) { [animationImages addObject:[UIImage imageNamed:imageName]]; } [self.imageView addAnimationWithImages:animationImages times:times repeatCount:1 animationFrame:CGRectMake(0, 0, image.size.width, image.size.height)];}
0 0
- 给UIImageView设置一组图片动画,每张图片停留时间不一祥
- UIImageView 播放一组图片动画
- UIImageView图片动画
- iOS LaunchScreen设置启动图片 启动页停留时间
- iOS LaunchScreen设置启动图片 启动页停留时间
- iOS LaunchScreen设置启动图片 启动页停留时间
- iOS LaunchScreen设置启动图片 启动页停留时间
- 使用一组图片播放动画
- UIImageView 实现一组动画
- iPhone 如何使用UIImageView播放动画,并停留在之后一张图片并添加播放结束时的事件
- iOS LaunchScreen设置启动图片并且设置启动页停留时间
- 给UIButton添加URL图片、给UIImageView添加URL图片
- iOS:UIimageView切换图片渐入渐出的动画
- 利用UIImageView连续播放图片动画效果
- UILabel—uiimageview----图片动画实现
- iOS:UIimageView切换图片渐入渐出的动画
- UIImageView 使用animationImages动画展示图片
- 图片数组形成动画(UIImageView)
- String、StringBuffer和StringBuilder类比较
- Unity3D学习成潮流:如何成为一位Unity3D开发者
- HDU 3695 (AC自动机)
- 如何用set命令设置Windows 7操作系统的环境变量
- 希尔排序
- 给UIImageView设置一组图片动画,每张图片停留时间不一祥
- opengl顶点着色器
- 列出mysql所有数据库表的存储引擎
- Fragment
- 设计模式:解释器
- Android--JNI的开发流程
- Apache Kafka 实战从零开始(一)
- Java对象创建与内存分布
- CAShapeLayer和CAGradientLayer