将此段代码放到自定义的UITabBarController脚本中即可
//---------------------------------------------------- // 当点击tabBar的时候,自动执行该代理方法(不需要手动设置代理) override func tabBar(_ tabBar: UITabBar,didSelect item: UITabBarItem) { // 使用枚举遍历,判断选中的tabBarItem等于数组中的第几个 for (k,v) in (tabBar.items?.enumerated())! { if v == item { // 打印选中的iteme下标 print(k) // 将下标传入动画方法 animationWithIndex(index: k) } } } //---------------------------------------------------- // 动画方法 func animationWithIndex(index:Int){ // 不知为何,无法设置数组类型为UITabBarButton??????所以设置成了Any var tabbarbuttonArray:[Any] = [Any]() for tabBarBtn in self.tabBar.subviews { if tabBarBtn.isKind(of: NSClassFromString("UITabBarButton")!) { tabbarbuttonArray.append(tabBarBtn) } } //---------------------------------------------------- let pulse = CABasicAnimation(keyPath: "transform.scale") pulse.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaSEOut) pulse.duration = 0.08 pulse.repeatCount = 1 pulse.autoreverses = true pulse.fromValue = 0.7 pulse.toValue = 1.3 // 给tabBarButton添加动画效果 let tabBarLayer = (tabbarbuttonArray[index] as AnyObject).layer tabBarLayer?.add(pulse,forKey: nil) } //----------------------------------------------------原文链接:https://www.f2er.com/swift/322791.html