我有一个带有多个 tableviewcell 的 tableview。每个单元格都有一个 UIActivityIndicatorView,它根据状态触发:
private func stateDidChange(duration: NSTimeInterval) {
switch self.state {
case .Failed:
self.testingIndicator.stopAnimating()
self.animateWithColor(self.redColor.CGColor)
break
case .Success:
self.testingIndicator.stopAnimating()
self.animateWithColor(self.greenColor.CGColor)
break
case .Testing:
self.testingIndicator.startAnimating()
self.animateWithColor(self.greyColor.CGColor)
break
case .Unknown:
self.testingIndicator.stopAnimating()
self.animateWithColor(self.greyColor.CGColor)
break
}
}
private func animateWithColor(color: CGColor) {
CATransaction.begin()
CATransaction.setAnimationDuration(self.duration)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
self.shapeLayer.strokeColor = color
CATransaction.commit()
}
第二个函数对指示器周围的圆圈的描边颜色进行动画处理。 我正在使用这个观察者来观察状态变化:
var state: TestStepModel.State {
didSet {
self.stateDidChange(self.duration)
}
}
到目前为止,一切都按预期进行,但如果只有一个单元格发生变化,我会更新每个单元格。 所以我将这段代码添加到观察者中:
var state: TestStepModel.State {
didSet {
if state != oldValue {
self.stateDidChange(self.duration)
}
}
}
突然间,一旦第一个状态发生变化,每个单元格中的每个事件指示器都会停止动画。 更糟糕的是,我可以像这样取消每个 .stopAnimating() 调用的注释:
switch self.state {
case .Failed:
//self.testingIndicator.stopAnimating()
self.animateWithColor(self.redColor.CGColor)
break
case .Success:
//self.testingIndicator.stopAnimating()
self.animateWithColor(self.greenColor.CGColor)
break
case .Testing:
self.testingIndicator.startAnimating()
self.animateWithColor(self.greyColor.CGColor)
break
case .Unknown:
//self.testingIndicator.stopAnimating()
self.animateWithColor(self.greyColor.CGColor)
break
}
并且在第一个状态更改后它仍然停止动画。是的,除了这三个(在整个项目中搜索)之外,没有其他 stopAnimating() 调用。
所以,是的,这当然是一个非常奇怪和具体的问题,但也许有人知道如何解决它或面临类似的问题...... 事件指示器应该仅在我调用 .stopAnimating() 时停止并隐藏,但当我触发状态更改时它会停止。
最佳答案
我遇到了同样的问题,我所做的就是停止重新加载 Collection View 。让我知道这是否有帮助,否则我也可以分享代码片段。
关于ios - swift ActivityIndicator 自动停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291356/