【问题标题】:Animate ProgressView in Cell在单元格中为 ProgressView 设置动画
【发布时间】:2016-08-09 19:38:48
【问题描述】:

我想在 CollectionViewCell 中添加一个 progressView。 ProgressView 应该是动画的,并在 10 秒内运行 von 1 到 0 的进度。

所以现在对每个进度视图都使用“viewWithTag”,并更新我的进度:

UIView.animateWithDuration(3, animations: { () -> Void in
    progressView.setProgress(1.0, animated: true)
})

但是,我想它必须容易得多。当生病重新加载单元格时,我想再次为进度设置动画。那么直接将该功能包含在我的单元格中是否应该更容易?

喜欢:

override func prepareForReuse() {
    super.prepareForReuse()
    print("prepareForReuse")
    UIView.animateWithDuration(3, animations: { () -> Void in
       progressView.setProgress(1.0, animated: true)
    })
}

但这不起作用,滚动时也应该是一个问题。有没有办法让单元格中的 progressView 动画精确 10 秒,并且(当你滚动时)progressView 仍然没有从头开始?

好的,当我更新我的单元格时,我现在开始我的动画:

func updateCell() {

    photos[2].comment = "dsadhjsa hdjksahdjsahfk jhasfjklha fjkhdjsk fhjksdf hjskdfh jkdshfjksdh fdhsjkfhdsjkf dsadsad dsad sad " // test Test 

    let h: Int = random() % 100;

    photos[2].height = 120 + CGFloat(h) // set custom height for test

    let path = NSIndexPath(forItem: 2, inSection: 0)

    self.collectionViewLayout.invalidateLayout()

    collectionView?.reloadItemsAtIndexPaths([path])

    if let progressView = view.viewWithTag(2) as? UIProgressView {

       progressView.setProgress(0, animated: false)

       UIView.animateWithDuration(4, animations: { () -> Void in
       progressView.layoutIfNeeded()
       progressView.setProgress(1, animated: true)

  })

}

}

这是有效的,但是(当然)当我向下滚动时,我的单元格会丢失动画。我想这将是存储当前进度值的更好方法,如果单元格再次可见,我会再次加载它吗?

或者还有其他可能性吗?

【问题讨论】:

  • 在动画块中添加 0.5 秒的延迟。
  • 当单元格被重用时,这对我有什么帮助(所以动画从头开始?)

标签: ios swift uicollectionview


【解决方案1】:
override func prepareForReuse() {
super.prepareForReuse()
print("prepareForReuse")
UIView.animateWithDuration(3, animations: { () -> Void in
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
  progressView.setProgress(1.0, animated: true)
   }
    })
}

试试这个。

【讨论】:

  • 谢谢 - 但不工作。我现在每 3 秒用一个计时器重新加载我的手机,第一次没问题,但再也不会了。并且将其设置为开始为 0。 dispatch_async(dispatch_get_main_queue()) { [unowned self] in self.progressView.setProgress(0, animated: false) }
  • 我正在用计时器重新加载我的手机。但我的问题是进度条以 100% 运行,然后保持在该水平。并且当生病使用prepareForReuse - 当生病滚动时,进度条会重新开始吗?
  • 所以这不是一个好主意 - 因为当滚动不良时,我的 progressView 将从头开始运行。
  • 我想在每个单元格中显示内容,并且此内容每 10 秒更新一次。这就是为什么这不是一个好主意。
  • 您签入 willDisplayCell 了吗?这是表格视图中的链接。检查你是否有任何想法stackoverflow.com/questions/29404586/…
猜你喜欢
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多