如何解决如何在自定义原型单元中为progressBar设置动画?
我正在尝试在自定义UITableViewCell中制作以下动画:
用户注销任务时,进度条应在5秒钟内从0.0变为1.0。
我尝试过的事情
使用TableVC中的委托方法制作动画:
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,itemID: itemID)
UITableViewCell.animate(withDuration: 5.0) {
self.progressBar.setProgress(1.0,animated: true)
}
}
}
这确实设置了进度条,但没有设置动画。进度栏突然更改。 This is what happens
任何人都可以照亮我做错了什么吗?我是错误地调用了动画函数,还是在错误的地方?
谢谢, 马特
解决方法
我刚刚创建了一个简化的示例,对我来说,以下代码有效:
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)
}
}
}
ViewController
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
}
}
当然,您需要正确的情节提要配置才能运行我的代码...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。