满足条件后如何快速停止活动指示器? [英] How to stop the activity indicator in swift after a condition is met?
问题描述
我已将 UIActivityIndicatorView 声明为模型扩展中的计算属性.我有一个方法 configureCell ,我试图在其中使用活动指示器作为 imageView 的子视图.在这里,我可以将指标置于特定条件下,但以后无法对其进行任何更改.例如,我无法停止活动指示器,无法更改颜色,甚至无法隐藏它.
I have declared a UIActivityIndicatorView as a computed property in extension of my model. I have a method configureCell where I'm trying to use activity indicator as a subview of an imageView. Here I'm able to position the indicator on certain condition but am not able to do any changes on it later. E.g I am not able to stop the activity indicator, not able to change the color, not able to even hide it.
extension TranscationModel: UITableViewDataSource, UITableViewDelegate
{
var activityIN: UIActivityIndicatorView {
var act = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
act.color = UIColor.redColor()
act.hidden = false
act.startAnimating()
return act
}
func configureTransactionCell(cell : TransactionCell?, indexPath: NSIndexPath) {
if transaction.tid == "Something" {
activityIN.color = UIColor.greenColor() //Even this doesn't work
activityIN.center = cell.imgTransactionBill.center
cell.imgTransactionBill.addSubview(activityIN)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
activityIN.stopAnimating() //Not working
activityIN.hidden = true //Not working
}
}
}
推荐答案
您将 activityIN
定义为计算变量.任何时候当你调用它时,你都会得到一个全新的实例.在 UIActivityIndicatorView
的便利初始值设定项中查看 activityIN
var.如果您只需要 10 秒钟的活动,请像这样编辑您的 func:
You defined activityIN
as computed variable. Any time when you call it, you get completely new instance. Look at activityIN
var as at convenience initializer of UIActivityIndicatorView
. If you only need activity for ten seconds, edit your func like this:
func configureTransactionCell(cell : TransactionCell?, indexPath: NSIndexPath) {
if transaction.tid == "Something" {
let weakActivityIndicator = activityIN
weakActivityIndicator.color = UIColor.greenColor()
weakActivityIndicator.center = cell.imgTransactionBill.center
cell.imgTransactionBill.addSubview(weakActivityIndicator)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
weakActivityIndicator.stopAnimating()
weakActivityIndicator.hidden = true
}
}
}
但通常你需要保持对从这个初始化器获得的实例的引用.在 TransactionCell
类中定义 UIActivityIndicatorView
存储属性.
But usually you need to keep reference to instance that you get from this initializer. Define UIActivityIndicatorView
stored property in your TransactionCell
class.
假设 TransactionCell
类有声明
var cellActivityIndicator: UIActivityIndicatorView!
然后你可以像这样编辑你的函数
then you can edit your func like this
func configureTransactionCell(cell : TransactionCell?, indexPath: NSIndexPath) {
if transaction.tid == "Something" {
cell.cellActivityIndicator = activityIN
cell.cellActivityIndicator.color = UIColor.greenColor()
cell.cellActivityIndicator.center = cell.imgTransactionBill.center
cell.imgTransactionBill.addSubview(cell.cellActivityIndicator)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
cell.cellActivityIndicator.stopAnimating()
cell.cellActivityIndicator.hidden = true
}
}
}
这篇关于满足条件后如何快速停止活动指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!