【问题标题】:WatchOS background color animationWatchOS 背景色动画
【发布时间】:2019-01-06 12:52:17
【问题描述】:

我正在尝试在 WatchKit 中制作一个简单的动画。 我想闪烁特定的颜色,然后回到黑色背景。一切正常……一次。在这第一次眨眼之后,什么都不会发生。我试过搜索,但找不到相关的答案。 我猜我应该以某种方式重置动画的状态,但找不到任何方法。

这是我用于动画的代码:

animate(withDuration: 0.2, animations: {
    //set first color
    //mainGroup is a WKInterfaceGroup
    self.mainGroup.setBackgroundColor(color)
    //set back black color
    self.mainGroup.setBackgroundColor(UIColor.black)
})

【问题讨论】:

    标签: swift watchkit watchos


    【解决方案1】:

    我已经研究了更多。 WatchKit 中的动画功能没有选项,这就是让它变得更难的原因(不能使用自动反转)。解决方案并不完美,但或多或​​少是有效的。您需要首先设置所需的“闪烁”颜色,然后将过渡动画设置为黑色。

    self.mainGroup.setBackgroundColor(color)
    animate(withDuration: 0.2, animations: {
        self.mainGroup.setBackgroundColor(UIColor.black)
    })
    

    【讨论】:

      【解决方案2】:

      如果您想要更复杂的时间,例如流畅的“in”动画,可以使用延迟执行:

      animate(withDuration: 0.2) {
          self.mainGroup.setBackgroundColor(color)
      } 
      DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.21) {
          animate(withDuration: 0.2) {
              self.mainGroup.setBackgroundColor(.black)
          }
      }
      

      【讨论】:

      • 我有类似的行为(动画只会运行一次),直到我将动画嵌入到 DispatchQueue.main.async() 块中。然后它就可靠地工作了。
      猜你喜欢
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 2010-09-30
      • 2012-12-18
      • 2023-03-11
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多