- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// viewController.tabBarItem.gs_imageView.transform = CGAffineTransformMakeScale(0.1,0.1);
// // 彈簧動(dòng)畫(huà),參數(shù)分別為:時(shí)長(zhǎng),延時(shí),彈性(越小彈性越大)役听,初始速度
// [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實(shí)現(xiàn)的表窘,所以遇到這個(gè)class就直接拿
if ([NSStringFromClass([subview class]) isEqualToString:@"UITabBarSwappableImageView"]) {
return (UIImageView *)subview;
}
// if (IOS_VERSION < 10) {
// // iOS10以前典予,選中的item的高亮是用UITabBarSelectionIndicatorView實(shí)現(xiàn)的,所以要屏蔽掉
// 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.獲取時(shí)長(zhǎng)
// 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;這句話會(huì)使得tabbar點(diǎn)擊加載GIF完后乐严,點(diǎn)擊其他項(xiàng)之前項(xiàng)變成灰色正方框的bug瘤袖。(原因大概是tabbar上的圖片本質(zhì)上不是一個(gè)圖片,而是一個(gè)形狀圖片昂验。系統(tǒng)對(duì)我們使用的圖片也只是把其中的形狀“扣”出來(lái)捂敌,其余的背景什么的都不要。因?yàn)槲覀兛赡芙o背景加了顏色既琴,所以系統(tǒng)扣的時(shí)候只是把背景扣出來(lái)了占婉,只看到一個(gè)方塊,而且還是系統(tǒng)處理過(guò)成灰色甫恩。
)
}
CGImageRelease(cgImage);
}
imgView.animationImages = imageArr;
imgView.animationDuration = duration;
imgView.animationRepeatCount = 1;
[imgView startAnimating];
}
//獲取GIF圖片每幀的時(shí)長(zhǎng)
- (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;
}