swift ios标签栏控制器动画点击

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift ios标签栏控制器动画点击相关的知识,希望对你有一定的参考价值。

import UIKit

class FunctionTabController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
   
    override func viewDidAppear(_ animated: Bool) {
        animationWithIndex(0)
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        // get number of select item
        for (k,v) in (tabBar.items?.enumerated())! {
            if v == item {
                animationWithIndex(k)
            }
        }
    }
    
    func animationWithIndex(_ index:Int){
        
        var tabbarbuttonArray: [Any] = [Any]()
        
        for tabBarBtn in self.tabBar.subviews {
            if tabBarBtn.isKind(of: NSClassFromString("UITabBarButton")!) {
                tabbarbuttonArray.append(tabBarBtn)
            }
        }
        
        // define animation
        let pulse = CABasicAnimation(keyPath: "transform.scale")
        pulse.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        pulse.duration = 0.08
        pulse.repeatCount = 1
        pulse.autoreverses = false
        pulse.fillMode = kCAFillModeForwards
        pulse.isRemovedOnCompletion = false
        pulse.fromValue = 0.7
        pulse.toValue = 1.1
        
        // add animation
        let tabBarLayer = (tabbarbuttonArray[index] as AnyObject).layer
        tabBarLayer?.add(pulse, forKey: "transform.scale")
        
        // remove other animation
        for i in 0...3{
            if i != index{
                let otherTabBarLayer = (tabbarbuttonArray[i] as AnyObject).layer
                otherTabBarLayer?.removeAllAnimations()
            }
        }
    }
}

以上是关于swift ios标签栏控制器动画点击的主要内容,如果未能解决你的问题,请参考以下文章

iOS:给标签栏控制器的UITabbarItem添加点击动效

如何使用 Swift 在 iOS 中创建类似于标签栏选择动画的 LinkedIn?

ios标签栏点击显示模态视图黑屏

ios 8/swift 标签栏多点点击图标小我怎么办? [关闭]

iOS Swift标签栏控制器以编程方式添加项目(新标签)

当用户直接点击标签栏项目时,如何禁用滑动动画?