YYKit---Image gif
帧动画 YYFrameImage 继承UIImage <YYAnimatedImage>
1.- (nullable instancetype)initWithImagePaths:(NSArray*)paths//图片路径
oneFrameDuration:(NSTimeInterval)oneFrameDuration//每张图片播放时间
loopCount:(NSUInteger)loopCount;//循环次数,0代表,不限次数
2.- (nullable instancetype)initWithImagePaths:(NSArray*)paths frameDurations:(NSArray*)frameDurations//@[ @0.1,@0.2,@0.3];
loopCount:(NSUInteger)loopCount;
也可以用NSData创建
3.- (nullable instancetype)initWithImageDataArray:(NSArray*)dataArray
oneFrameDuration:(NSTimeInterval)oneFrameDuration
loopCount:(NSUInteger)loopCount;
4.- (nullable instancetype)initWithImageDataArray:(NSArray*)dataArray
frameDurations:(NSArray *)frameDurations
loopCount:(NSUInteger)loopCount;
ps.只支持png and jpeg
YYImage 继承UIImage <YYAnimatedImage>
YYImage *image = [YYImage imageNamed:name];//这样就可以加载本地的图片和GIF ,webP的动态图
YYSpriteSheetImage 继承UIImage <YYAnimatedImage>
用于截取图片,然后播放动态图。代码如下:
CGSize size = CGSizeMake(sheet.size.width / 8, sheet.size.height / 12);//12 行8列的sheet(UIImage)
for (int j = 0; j < 12; j++) {
for (int i = 0; i < 8; i++) {
CGRect rect;
rect.size = size;
rect.origin.x = sheet.size.width / 8 * i;
rect.origin.y = sheet.size.height / 12 * j;
[contentRects addObject:[NSValue valueWithCGRect:rect]];//NSValue 类型的
[durations addObject:@(1 / 60.0)];//NSNumber 类型
}
}
YYSpriteSheetImage *sprite;
sprite = [[YYSpriteSheetImage alloc] initWithSpriteSheetImage:sheet contentRects:contentRects frameDurations:durations loopCount:0];
YYAnimatedImageView 继承UIImageView
- (BOOL)isAnimating; 是否在动画
- (void)startAnimating;开始动画
- (void)stopAnimating;停止动画 //都是UIImageView 的方法
加载网络图片和SDWebImage 差不多
YYKit 有一个UIImageView 和UIButton的分类 可以加载网络图片,png,等等
- (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder;
想实现点击播放,点击停止,是需要给YYAnimatedImageView
添加tap手势。判断是否正在动画,来决定开始还是停止动画。
实现左滑右滑,前进后退。YYImage等都遵守协议.
+ (void)addPanControlToAnimatedImageView:(YYAnimatedImageView *)view {
if (!view) return; //
view.userInteractionEnabled = YES;
__weak typeof(view) _view = view;
__block BOOL previousIsPlaying; //记录滑动前是否动画
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithActionBlock:^(id sender) {
UIImage*image = (id)_view.image;
if (![image conformsToProtocol:@protocol(YYAnimatedImage)]) return;//image 是否遵守协议
UIPanGestureRecognizer *gesture = sender;
CGPoint p = [gesture locationInView:gesture.view];
CGFloat progress = p.x / gesture.view.width;
if (gesture.state == UIGestureRecognizerStateBegan) {//手势开始时,记录当前是否动画,并
previousIsPlaying = [_view isAnimating]; 停止动画。把当前在的index赋值给
[_view stopAnimating]; View
_view.currentAnimatedImageIndex = image.animatedImageFrameCount * progress;
}
else if (gesture.state == UIGestureRecognizerStateEnded ||gesture.state == UIGestureRecognizerStateCancelled) {//手势结束时,恢复原先的状态
if (previousIsPlaying) [_view startAnimating];
} else {//移动的时候,把当前的index服给View
_view.currentAnimatedImageIndex = image.animatedImageFrameCount * progress;
}
}];
[view addGestureRecognizer:pan];
}