美文网首页iOS开发
原生tabbar动态加载gif,单次播放

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

作者: 又又轻 | 来源:发表于2019-03-05 14:39 被阅读0次
    - (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<count; i++) {
            CGImageRef cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, (CFDictionaryRef)gifCountDic);
            NSTimeInterval frameDuration = [self gifImageDeleyTime:imageSource index:i];
            duration += frameDuration;
                // 3.2.获取时长
    //        guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) , let gifInfo = (properties as NSDictionary)[kCGImagePropertyGIFDictionary as String] as? NSDictionary,
    //        (gifInfo[kCGImagePropertyGIFDelayTime as String] as? NSNumber) else { continue }
            UIImage *imageName =  [UIImage imageWithCGImage:cgImage];
            [imageArr addObject:imageName];
            if (i == count-1) {
    //            imgView.image = imageName;这句话会使得tabbar点击加载GIF完后,点击其他项之前项变成灰色正方框的bug。(原因大概是tabbar上的图片本质上不是一个图片,而是一个形状图片。系统对我们使用的图片也只是把其中的形状“扣”出来,其余的背景什么的都不要。因为我们可能给背景加了颜色,所以系统扣的时候只是把背景扣出来了,只看到一个方块,而且还是系统处理过成灰色。
    )
              
            }
            CGImageRelease(cgImage);
        }
        imgView.animationImages = imageArr;
        imgView.animationDuration = duration;
        imgView.animationRepeatCount = 1;
        [imgView startAnimating];
    }
        //获取GIF图片每帧的时长
    - (NSTimeInterval)gifImageDeleyTime:(CGImageSourceRef)imageSource index:(NSInteger)index {
        NSTimeInterval duration = 0;
        CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, NULL);
        if (imageProperties) {
            CFDictionaryRef gifProperties;
            BOOL result = CFDictionaryGetValueIfPresent(imageProperties, kCGImagePropertyGIFDictionary, (const void **)&gifProperties);
            if (result) {
                const void *durationValue;
                if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFUnclampedDelayTime, &durationValue)) {
                    duration = [(__bridge NSNumber *)durationValue doubleValue];
                    if (duration < 0) {
                        if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFDelayTime, &durationValue)) {
                            duration = [(__bridge NSNumber *)durationValue doubleValue];
                        }
                    }
                }
            }
        }
        
        return duration;
    }
    
    
    

    相关文章

      网友评论

        本文标题:原生tabbar动态加载gif,单次播放

        本文链接:https://www.haomeiwen.com/subject/bfanuqtx.html