iOS Swift tabbarItem点击时的动画效果

1.实现动画效果的方法

/// tabbarItem点击时的动画效果
    ///
    /// - Parameter index: 点击的item的位置
    func tabbarItemAnimationWithIndex(index: Int){
        var tabbarItem : [UIView] = []
        for tabBarButton in self.tabBar.subviews{
            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!){
               tabbarItem.append(tabBarButton)
            }
        }
        let pulse = CABasicAnimation.init(keyPath: "transform.scale")
        pulse.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
        pulse.duration = 0.1
        pulse.repeatCount = 1
        pulse.fromValue = NSNumber.init(value: 0.8)
        pulse.toValue = NSNumber.init(value: 1.0)
        let itemView = tabbarItem[index]
        itemView.layer.add(pulse, forKey: "")
        self.indexFlag = index//声明的私有属性
    }

2.重写tabbarItem点击的方法

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let selectIndex = self.tabBar.items?.index(of: item)
        //声明的私有属性
        if self.indexFlag != selectIndex{
           self.tabbarItemAnimationWithIndex(index: selectIndex!)
        }
    }

你可能感兴趣的:(iOS Swift tabbarItem点击时的动画效果)