animation显示button

我正在尝试创build一个基本的animation。 我需要触摸一个button来隐藏或显示。

我编写了这个代码来点击屏幕:

func visibleControlButton(_ sender: UITapGestureRecognizer) { if (backButton!.isHidden) { _UIButtonHiddenAnimation.hiddenAnimation(button: self.backButton!, hide: false) } else { _UIButtonHiddenAnimation.hiddenAnimation(button: self.backButton!, hide: true) } } 

定义_UIButtonHiddenAnimation:

  class _UIButtonHiddenAnimation { class func hiddenAnimation(button: UIButton, hide: Bool) { UIView.animate(withDuration: 0.2, animations: { hide ? (button.alpha = 0) : (button.alpha = 1.0) }, completion: { finished in hide ? (button.isHidden = true) : (button.isHidden = false) }) } } 

animation只是隐藏button。 如何制作一个button的animation外观?

问题是,如果button被隐藏,则将alpha设置为1,但我们看不到 – 因为button被隐藏了! 然后,您将isHidden设置为false并跳转到视图中。

解决scheme:忘记所有关于isHidden ,只改变alpha – 然后改变你的testing,以匹配你正在做的button,即简单地testing其alpha值。 因此(整理整理):

 class _UIButtonHiddenAnimation { class func hiddenAnimation(button: UIButton, hide: Bool) { UIView.animate(withDuration: 0.2, animations: { button.alpha = hide ? 0 : 1.0 }) } } func visibleControlButton(_ sender: UITapGestureRecognizer) { _UIButtonHiddenAnimation.hiddenAnimation( button: self.backButton!, hide: backButton!.alpha > 0.1) }