简体   繁体   English

WatchOS 背景色动画

[英]WatchOS background color animation

I'm trying to make a simple animation in WatchKit.我正在尝试在 WatchKit 中制作一个简单的动画。 I want to blink the specific color, and then come back to black background.我想闪烁特定的颜色,然后回到黑色背景。 It all works fine... once.一切正常……一次。 After this first blink nothing will happen.在第一次眨眼之后,什么都不会发生。 I've tried searching but couldn't find relevant answer.我试过搜索,但找不到相关的答案。 My guess it that I should somehow reset the state of the animation, but couldn't find any way to do that.我猜我应该以某种方式重置动画的状态,但找不到任何方法来做到这一点。

This is the code I'm using for the animation:这是我用于动画的代码:

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)
})

I've researched a bit more.我研究了一点。 There are no options in animate function in WatchKit, and that is what makes it harder (can't use autoreverse). WatchKit 中的 animate 函数没有选项,这就是让它变得更难的原因(不能使用 autoreverse)。 The solution is not perfect, but it more or less works.该解决方案并不完美,但或多或​​少是有效的。 You need to first set the desired 'blink' color, and after that animate the transition to black.您需要首先设置所需的“闪烁”颜色,然后将过渡动画设置为黑色。

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

If you want a more complicated timing, eg smooth "in" animation, you can use delayed execution:如果你想要一个更复杂的时间,例如平滑的“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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM