代码之家  ›  专栏  ›  技术社区  ›  Jitendra Tanwar

如何在swift中设置tabbar徽章计数的动画

  •  1
  • Jitendra Tanwar  · 技术社区  · 9 年前

    1 回复  |  直到 9 年前
        1
  •  4
  •   Ali Adam    9 年前

    在与您共享代码之前,我会做一些类似的事情 首先,我创建了两个函数 第一个是:

    func loopThrowViews(view:UIView){
        for subview in (view.subviews){
            let type = String(describing: type(of: subview))
            print(type)
            if type == "_UIBadgeView" {
                print("this is BadgeView")
                animateView(view: subview)
            }
             else {
                loopThrowViews(view:subview)
            }
    
        }
    }
    

    此函数获取视图并循环抛出其所有子视图,直到找到徽章视图,然后将动画方法称为此方法

      func animateView(view:UIView){
    let shakeAnimation = CABasicAnimation(keyPath: "position")
    shakeAnimation.duration = 0.05
    shakeAnimation.repeatCount = 50
    shakeAnimation.autoreverses = true
    shakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x:view.center.x - 10, y:view.center.y))
    shakeAnimation.toValue = NSValue(cgPoint: CGPoint(x:view.center.x + 10, y:view.center.y))
    view.layer.add(shakeAnimation, forKey: "position")
    }
    

    您可以用自己的动画替换此方法中的代码

    当你想要制作徽章的动画时,你所需要的就是这样调用这个方法

     loopThrowViews(view: self.tabBarController!.tabBar)
    

    enter image description here

    此处为完整示例 https://github.com/AliAdam/AnimateTabbarBadgeView