原生tabbar动态加载gif,单次播放

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//    viewController.tabBarItem.gs_imageView.transform = CGAffineTransformMakeScale(0.1,0.1);
//    // 弹簧动画,参数分别为:时长,延时,弹性(越小弹性越大),初始速度
//    [UIView animateWithDuration: 0.7 delay:0.1 usingSpringWithDamping:0.25 initialSpringVelocity:0.3 options:0 animations:^{
//        // 放大
//        viewController.tabBarItem.gs_imageView.transform = CGAffineTransformMakeScale(1,1);
//    } completion:nil];
//    NSString *str = [[NSBundle mainBundle]pathForResource:@"home" ofType:@"gif"];
//
//    NSData *data = [NSData dataWithContentsOfFile:str];
//
//    viewController.tabBarItem.gs_imageView.image =  [UIImage sd_animatedGIFWithData:data];//[UIImage sd_animatedGIFWithData:data];
    NSString *gifName = @"";
    if (self.selectedIndex == 0) {
        gifName = @"home";
    }
    else if (self.selectedIndex == 1) {
        gifName = @"epluse";
    }
    else if (self.selectedIndex == 4) {
        gifName = @"Mine";
    }
    if (gifName.length>0) {
        VDGifPlayerTool *addGif =  [[VDGifPlayerTool alloc]init];
        [addGif startAnimateGifMethod:gifName toView:viewController.tabBarItem.gs_imageView];

    }
   
}

其中gs_imageView是为了使中间的tabbar凸出的修改图片位置的其他文件

#import "UITabBarItem+XKTabBarItem.h"
#import "UIBarItem+XKBarItem.h"

@implementation UITabBarItem (XKTabBarItem)

- (UIImageView *)gs_imageView {
    return [self.class gs_imageViewInTabBarButton:self.gs_view];
}

+ (UIImageView *)gs_imageViewInTabBarButton:(UIView *)tabBarButton {
    
    if (!tabBarButton) {
        return nil;
    }
    
    for (UIView *subview in tabBarButton.subviews) {
        // iOS10及以后,imageView都是用UITabBarSwappableImageView实现的,所以遇到这个class就直接拿
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITabBarSwappableImageView"]) {
            return (UIImageView *)subview;
        }
        
//        if (IOS_VERSION < 10) {
//            // iOS10以前,选中的item的高亮是用UITabBarSelectionIndicatorView实现的,所以要屏蔽掉
//            if ([subview isKindOfClass:[UIImageView class]] && ![NSStringFromClass([subview class]) isEqualToString:@"UITabBarSelectionIndicatorView"]) {
//                return (UIImageView *)subview;
//            }
//        }
    }
    return nil;
}
//gif名字  加载在tabbaritem上
-(void)startAnimateGifMethod:(NSString *)name toView:(UIImageView *)imgView{
     NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
      NSDictionary *gifCountDic = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary];
     NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]];
     CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)gifData, (CFDictionaryRef)gifCountDic);
     size_t count = CGImageSourceGetCount(imageSource);
    NSTimeInterval duration = 0;
    NSMutableArray *imageArr = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i

你可能感兴趣的:(原生tabbar动态加载gif,单次播放)