Tabbar点击时候超炫小动画

废话就不多说了直接上效果图,这是我公司项目中加的动画,自己可以适当的加以修改称自己想要的

效果

1.gif

点击下面的按钮时候是不是有动画 可能这个Gif动画有延迟 效果不明显 下面上代码。。

代码

// 系统方法
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    NSInteger index = [self.tabBar.items indexOfObject:item];
     // indexFlag 可以注释
    if (self.indexFlag != index) {
        [self animationWithIndex:index];
    }
    
}
// 动画
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
    
    self.indexFlag = index;
    
}

// swift版本
class MainTabBar: UITabBarController {

var indexFlag = 0

override func viewDidLoad() {
super.viewDidLoad()
}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let index = tabBar.items?.index(of: item) {
if indexFlag != index {
animationWithIndex(index: index)
}
}
}

func animationWithIndex(index: Int) {
var arr = UIView
for tabBarButton in tabBar.subviews {
if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!) {
arr.append(tabBarButton)
}
}
let pulse = CABasicAnimation(keyPath: "transform.scale")
pulse.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
pulse.duration = 0.08
pulse.repeatCount = 1
pulse.autoreverses = true
pulse.fromValue = NSNumber(value: 0.7)
pulse.toValue = NSNumber(value: 1.3)
arr[index].layer.add(pulse, forKey: nil)

indexFlag = index
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

你可能感兴趣的:(Tabbar点击时候超炫小动画)