OGeek|极客世界-中国程序员成长平台

标题: ios - 在单元格中为 ProgressView 设置动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:13
标题: ios - 在单元格中为 ProgressView 设置动画

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

所以现在对每个进度 View 都使用“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)

  })

}

}

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

或者还有其他可能性吗?



Best Answer-推荐答案


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

试试这个。

关于ios - 在单元格中为 ProgressView 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38848146/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4