【问题标题】:Cannot get closure syntax to work in swift 4无法让闭包语法在 swift 4 中工作
【发布时间】:2017-11-08 17:02:09
【问题描述】:

我已经尝试了所有可能的语法变化,但完成处理程序总是在动画结束之前激活。我想我应该用别的东西代替Bool?

UIView.transition(with: swipeForPicturesIndicator,
                              duration: 1,
                              options: .curveEaseIn,
                              animations: {
                                self.swipeForPicturesIndicator.alpha = 0
            },
                              completion: { (Bool) -> Void in
                self.swipeForPicturesIndicator.isHidden = true
                self.swipeForPicturesIndicator.alpha = 0.8
            })

【问题讨论】:

  • 是否立即调用完成闭包?
  • 你在运行其他动画吗? Bool 将告诉您动画是否已取消或未完成 (false),或者是否成功完成 (true)。见:stackoverflow.com/questions/8686922/…
  • 您在屏幕上实际看到了什么以及您期望什么?
  • 根据描述,我怀疑目标是让指标逐渐淡出(alpha 为 0),1 秒后,指标被合法隐藏。这里的说法是该指标立即被隐藏(即没有褪色)。
  • 也就是说,我认为您实际上可能想要使用动画方法而不是过渡方法。

标签: ios swift syntax swift4 completionhandler


【解决方案1】:

Bool 值表示调用完成块时动画是否完成。如果该值为false,则表示您的动画已中断。

您应该使用animate 而不是transition

    UIView.animate(withDuration: 1,
                   delay: 0,
                   options: .curveEaseIn,
                   animations: {
      self.swipeForPicturesIndicator.alpha = 0
    }) { (completed) in

      /* Optionally check if animation finished */

      self.swipeForPicturesIndicator.isHidden = true
      self.swipeForPicturesIndicator.alpha = 0.8
    }

【讨论】:

    【解决方案2】:

    试试这个

    self.swipeForPicturesIndicator.alpha = 0
    UIView.transition(with: swipeForPicturesIndicator,
                                  duration: 1,
                                  options: .curveEaseIn,
                                  animations: {
    
                    self.swipeForPicturesIndicator.alpha = 0.8
    
                },
                                  completion: nil)
    

    【讨论】:

      【解决方案3】:

      您可能想要使用动画而不是过渡:

      UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: { 
              self.swipeForPicturesIndicator.alpha = 0
          }) { (success) in
              self.swipeForPicturesIndicator.isHidden = true
              self.swipeForPicturesIndicator.alpha = 0.8
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-25
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 2012-01-28
        相关资源
        最近更新 更多