swift activityIndi cator自动停止动画

问题描述 投票:0回答:1

我有一个包含多个tableviewcells的tableview。每个单元格都有一个UIActivityIndi​​catorView,它根据状态触发:

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

第二个函数为指针周围的圆圈设置strokeColor。我正在使用这个观察者来观察状态变化:

 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()时停止并隐藏,但它当我触发状态改变时停止。

ios swift animation tableview uiactivityindicatorview
1个回答
0
投票

我遇到了同样的问题,我所做的就是停止重新加载collectionview。让我知道,如果这有助于其他我也可以分享代码片段。

© www.soinside.com 2019 - 2024. All rights reserved.