【问题标题】:iOS Label Visibility toggle not animatingiOS标签可见性切换没有动画
【发布时间】:2016-04-07 04:26:41
【问题描述】:

我正在尝试根据 UIImageView 上的点击手势切换 UILabel 的可见性。执行切换的代码如下:

func imageTapped(img: UIImageView) {
    print(photoTitle.hidden)
    if (photoTitle.hidden) {
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.photoTitle.alpha = 1
            }, completion: nil)
    }
    else {
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.photoTitle.alpha = 0
            }, completion: nil)
    }
    self.photoTitle.hidden = !self.photoTitle.hidden
}

问题在于它似乎忽略了第二次点击时的动画,即再次隐藏 UILabel。它只是变得不可见,而不是逐渐动画化。在 viewdDidLoad() 中,我将 photoTitle.hidden = true 初始化为最初不可见。

有什么明显的错误吗?

【问题讨论】:

  • 语句 self.photoTitle.hidden = !self.photoTitle.hidden 在执行您的块的语句之前执行。
  • 如果只是为了隐藏标签,那么你应该只使用该标签的 alpha 而不是也使用 hidden 属性,alpha 也会给你效果
  • 如果您有任何理由隐藏标签,只需使用alpha 即可隐藏它。不要使用hidden

标签: ios swift cocoa-touch


【解决方案1】:

您需要将 self.photoTitle.hidden = true 移动到 else 条件的完成块中

【讨论】:

    【解决方案2】:

    hidden不适用于此动画,您可以代替alpha

    【讨论】:

      【解决方案3】:

      试着改成这样的功能

      斯威夫特 2

      func imageTapped(img: UIImageView) {
          print(photoTitle.hidden)
          UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
              self.photoTitle.alpha = self.photoTitle.alpha < 0.5 ? 1.0 : 0.0
          }, completion: nil)
      }
      

      斯威夫特 3、4、5

      func imageTapped(img: UIImageView) {
          print(photoTitle.hidden)
          UIView.animate(withDuration: 0.5, delay: 0, options: UIView.AnimationOptions.curveEaseInOut, animations: {
              self.photoTitle.alpha = self.photoTitle.alpha < 0.5 ? 1.0 : 0.0
          }, completion: nil)
      }
      

      【讨论】:

      • 是的,这行得通。我不明白我的代码有什么问题。你能解释一下吗?
      • 您的代码在动画完成之前更改了标签的隐藏状态,因为动画的持续时间为 0.5 秒,同时执行此“self.photoTitle.hidden = !self.photoTitle.hidden”指令
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多