【发布时间】:2017-02-10 22:15:47
【问题描述】:
我有一个计时器,我在闭包中运行它来帮助执行一些 ui 更新(进度条)。它适用于第一个项目,但我希望它适用于多个项目。因此,每次运行闭包时,都会运行一个新的计时器实例并运行新的 userInfo。
// dispatch and add run loop to get it to fire
DispatchQueue.main.async(execute: {
timer = Timer.init(timeInterval: 0.25, target: self, selector: #selector(self.downloadTimer(_:)), userInfo: [innerCell, collectionView, indexPath], repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
})
当下面的这个函数完成后,我希望重新创建计时器并将新的 userInfo 传递给选择器..
func downloadTimer(_ timer: Timer) {
// need to pass arguments to timer as array inside userInfo
// then create dictionary and extract
let dict = timer.userInfo as! NSArray
let cell = dict[0] as! InnerCollectionCell
let collectionView = dict[1] as! UICollectionView
let indexPath = dict[2] as! IndexPath
// the index path isnt getting reset
// without casting the progress bar in each cell was displaying when coming back to DownloadCollectionController
if let downloadingCell = collectionView.cellForItem(at: indexPath) as? InnerCollectionCell {
DispatchQueue.main.async(execute: {
downloadingCell.progressBar.isHidden = false
downloadingCell.contentView.bringSubview(toFront: cell.progressBar)
downloadingCell.progressBar.setProgress(downloadProgress, animated: true)
downloadingCell.setNeedsDisplay()
if downloadProgress >= 1.0 {
print("TIMER STOPPED")
downloadingCell.progressBar.isHidden = true
//downloadQueue.operations[0].cancel()
timer.invalidate()
collectionView.reloadItems(at: [indexPath])
}
})
}
}
包含 timer = Timer 的闭包重新运行,但新的用户信息没有传递给计时器,它只是继续使用原始的 userInfo 运行。如果我使计时器无效,它就不能再被触发了?
我尝试在初始触发时在本地创建它:
var timer = Timer()
timer = Timer.init(timeInterval: 0.25, target: self, selector: #selector(self.downloadTimer(_:)), userInfo: [innerCell, collectionView, indexPath], repeats: true)
但它不会重新创建计时器,如果用户离开视图控制器,我希望能够停止它。
有没有更好的方法来实现这一点?我知道你不应该让计时器退役。每次都必须有一种方法来创建它的唯一实例。
---- 编辑----
这是索引路径打印的内容:
[0, 5]
[0, 5]
[0, 5]
[0, 5]
[0, 2]
[0, 5]
[0, 2]
[0, 1]
[0, 1]
[0, 1]
[0, 1]
因此它需要一段时间才能使计时器无效,并且在那个时候计时器已被重新触发,并且该函数使用旧的索引路径,直到进度 = 1
---- 编辑----
唯一发生失效的地方是进度 >= 1
if downloadProgress >= 1.0 {
timer.invalidate()
downloadingCell.progressBar.isHidden = true
downloadingCell.progressBar.progress = 0
collectionView.reloadData()
}
然后因为如果单元格满足某些条件,则从 cellForItemAt 触发计时器,当调用 collectionView.reloadData() 时,应重新验证计时器并发送更新的 userInfo。
这是 cellForItemAt 中的块:
for operation in downloadQueue.operations {
if operation.name == self.multiPartArray[collectionView.tag][indexPath.item].name {
// edits for all queued operations
innerCell.spinner.isHidden = false
innerCell.spinner.startAnimating()
innerCell.contentView.bringSubview(toFront: innerCell.spinner)
innerCell.spinner.activityIndicatorViewStyle = .gray
innerCell.spinner.color = UIColor.darkGray
// hide the progress on all in queue it will be pushed to front when needed
innerCell.progressBar.isHidden = true
if operation.name == downloadQueue.operations[0].name {
// edits for the active downlaod in queue
// hide the spinner for currently downloading cell
// innerCell.spinner.isHidden = true
innerCell.spinner.isHidden = false
innerCell.spinner.startAnimating()
innerCell.contentView.bringSubview(toFront: innerCell.spinner)
innerCell.spinner.activityIndicatorViewStyle = .gray
innerCell.spinner.color = UIColor.blue
DispatchQueue.main.async(execute: {
let timer = Timer.init(timeInterval: 0.5, target: self, selector: #selector(self.downloadTimer(_:)), userInfo: [innerCell, collectionView, indexPath, collectionView.tag], repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
})
break
} else {
// edits spinner that isnt currently downloading
innerCell.progressBar.isHidden = true
innerCell.spinner.color = UIColor.darkGray
}
} else {
// edits the spinner that isnt in queue
innerCell.progressBar.isHidden = true
innerCell.spinner.color = UIColor.darkGray
}
}
我意识到这不是最优雅的解决方案,但我有嵌套的集合视图,可能没有完全正确设置,所以在这种情况下不适合解决。
还有下载定时器:
func downloadTimer(_ timer: Timer) {
// need to pass arguments to timer as array inside userInfo
// then create dictionary and extract
let dict = timer.userInfo as! NSArray
let cell = dict[0] as! InnerCollectionCell
let collectionView = dict[1] as! UICollectionView
let indexPath = dict[2] as! IndexPath
let tag = dict[3] as! Int
print(indexPath)
// without casting the progress bar in each cell was displaying when coming back to DownloadCollectionController
if let downloadingCell = collectionView.cellForItem(at: indexPath) as? InnerCollectionCell {
// self.outerCollectionView.reloadItems(at: [IndexPath(item: tag, section: 0)])
DispatchQueue.main.async(execute: {
downloadingCell.spinner.isHidden = true
downloadingCell.progressBar.isHidden = false
downloadingCell.contentView.bringSubview(toFront: cell.progressBar)
downloadingCell.progressBar.setProgress(downloadProgress, animated: true)
downloadingCell.setNeedsDisplay()
if downloadProgress >= 1.0 {
timer.invalidate()
downloadingCell.progressBar.isHidden = true
downloadingCell.progressBar.progress = 0
collectionView.reloadData()
}
})
}
}
【问题讨论】: