Ios 如何在自定义原型单元中设置progressBar的动画?
我正在尝试在自定义UITableViewCell中设置以下动画: 当用户签出任务时,progressBar应在5秒内从0.0变为1.0 我尝试过的 在表格VC中的委托方法中执行动画:Ios 如何在自定义原型单元中设置progressBar的动画?,ios,swift,uitableview,animation,Ios,Swift,Uitableview,Animation,我正在尝试在自定义UITableViewCell中设置以下动画: 当用户签出任务时,progressBar应在5秒内从0.0变为1.0 我尝试过的 在表格VC中的委托方法中执行动画: let cell = tableView.cellForRow(at: IndexPath(item: indexSection!, section: indexRow!)) as? TaskCell cell?.progressBar.setProgress(1.0, animated: true) 这不起作用
let cell = tableView.cellForRow(at: IndexPath(item: indexSection!, section: indexRow!)) as? TaskCell
cell?.progressBar.setProgress(1.0, animated: true)
这不起作用,因为该单元格似乎不存在(print(cell!)
给出了一个致命错误)
在TaskCell.swift中制作动画
@IBAction func checkBoxAction(_ sender: Any) {
if items![indexRow!].checked {
delegate?.changeButton(state: false, indexSection: indexSection!, indexRow: indexRow!, itemID: itemID)
UIView.self.animate(withDuration: 1.0) {
self.progressBar.setProgress(0.0, animated: true)
}
} else {
delegate?.changeButton(state: true, indexSection: indexSection!, indexRow: indexRow!, itemID: itemID)
UITableViewCell.animate(withDuration: 5.0) {
self.progressBar.setProgress(1.0, animated: true)
}
}
}
这会设置进度条,但不会设置进度条的动画。进度条突然改变
谁能告诉我我做错了什么?我调用动画函数的方式是否有误,或者是在错误的位置调用
谢谢,
Matt我刚刚创建了一个简化的示例,以下代码对我有效: CustomTableViewCell
class CustomTableViewCell: UITableViewCell
@IBOutlet weak var progressView: UIProgressView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func startButtonDidTap(_ sender: UIButton) {
UIView.animate(withDuration: 2) {
self.progressView.setProgress(0.5, animated: true)
}
}
}
视图控制器
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "reuseID", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }
return cell
}
}
当然,为了运行我的代码,您需要一个正确的故事板配置…我已经上传了我的示例项目:谢谢finebel!这对我帮助很大。我试图实现您的代码,但问题似乎在于调用代码的位置。当我从开始iAction调用startActionII()时,它工作得非常好。但是,当我从委托方法调用该函数时,它不会设置动画。有什么想法吗?