代码之家  ›  专栏  ›  技术社区  ›  MEH

如何用动画更改导航栏的背景图像?

  •  0
  • MEH  · 技术社区  · 9 年前

    我正在使用一个大家都知道的动画,动画来改变导航栏的背景图像,并稍微延迟,将其从半透明(我将其设置为默认设置)更改为(蓝色主题)。

    UIView.animate(withDuration:0.5, 
                   delay: 0, 
                   option: UIViewAnimationOptions.curveEaseOut, 
                   animations: {
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
            self.navigationController?.navigationBar.shadowImage = UIImage()
        }, completion: nil)
    

    有什么想法吗?

    1 回复  |  直到 9 年前
        1
  •  9
  •   Hodson    9 年前

    你需要打电话 layoutIfNeeded() 在动画块内,例如。

    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 
        self.navigationController?.navigationBar.barTintColor = .blue
        self.navigationController?.navigationBar.layoutIfNeeded()
    }, completion: nil)
    

    更改背景图像的更新

    let animation = CATransition()
    animation.duration = 0.5
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
    animation.type = kCATransitionFade
    
    navigationController?.navigationBar.layer.add(animation, forKey: nil)
    
    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your-image-name"), for: .default)
    }, completion: nil)