In the usage scenario, the effect of gift giving in the live broadcast. After clicking send gift, you need to display the dynamic graph GIF
The current implementation scheme is to obtain the URL of the GIF graph from the server interface, and then start the display. After the display is completed, remove the picture
In podfile
pod 'YYKit'
Core code
#import "YYKit.h"
YYImage * image = [[YYImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.gif]]];
YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(Screen_Width / 2 - 300 / 2, 0, 300, 300);
imageView.backgroundColor = [UIColor clearColor];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[RACObserve(imageView, currentAnimatedImageIndex) subscribeNext:^(id _Nullable x) {
NSLog(@"play current page = %@",x);
if ([x integerValue] == imageView.animationImages.count) {
NSLog(@"play inner page = %@",x);
[imageView stopAnimating];
[imageView removeFromSuperview];
}
}];
[self.view addSubview:imageView];
[imageView startAnimating];
Another implementation scheme (applicable to the scheme with less frames)
When the number of GIF frames is too small, you need to play the GIF circularly without stopping playing. In this case, you can control the display and hiding through time
At this point, you can remove the RAC listening method
// [RACObserve(imageView, currentAnimatedImageIndex) subscribeNext:^(id _Nullable x) {
// NSLog(@"play current page = %@",x);
// if ([x integerValue] == imageView.animationImages.count) {
// NSLog(@"play inner page = %@",x);
// [imageView stopAnimating];
// [imageView removeFromSuperview];
//}
//}];
And add the following code
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//Code that requires deferred execution
[imageView removeFromSuperview];
});
The first scheme is applicable when there are many frames or there are strict requirements on execution time,
For example, the animation content is to dynamically display the three words “love you”. It’s embarrassing that the animation can’t be removed as soon as the display of the word “love” is over