我在 UITableView
中添加了一个动画,但是当它第一次加载时,它不符合我设置的约束。它们有点被挤压 - 单元格的高度和宽度。
动画位于UITableView
、willDisplay cell
的委托(delegate)方法中,主体如下所示:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.alpha = 0
cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1)
cell.layoutIfNeeded()
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
cell.alpha = 0.5
cell.layer.transform = CATransform3DMakeScale(1.05, 1.05, 1)
cell.layoutIfNeeded()
},completion: { finished in
UIView.animate(withDuration: 0.1, animations: {
cell.alpha = 1
cell.layer.transform = CATransform3DMakeScale(1, 1, 1)
cell.layoutIfNeeded()
})
})
}
我对 UITableView 的约束设置为 superView,它们占据了它的整个空间:
private func setupConstraints() {
let topVehiclesTableViewConstraint = vehiclesTableView.topAnchor.constraint(equalTo: view.topAnchor)
let bottomVehiclesTableViewConstraint = vehiclesTableView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor)
let leadingVehiclesTableViewConstraint = vehiclesTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailingVehiclesTableViewConstraint = vehiclesTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
view.addConstraints([topVehiclesTableViewConstraint, bottomVehiclesTableViewConstraint, leadingVehiclesTableViewConstraint, trailingVehiclesTableViewConstraint])
}
方法 setupConstraints()
在方法 : setupViews()
中调用,该方法在以下位置调用:
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
自定义单元格中所需的功能:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
编辑
它加载的次数与我有可见细胞动画的次数一样多。因此,如果我有 3-4 个单元格,那么动画将重复 3-4 次。这不是我想要的。
有人知道我该如何修复这个错误吗?
提前致谢!
最佳答案
试试这个:
在willDisplay委托(delegate)方法中添加
cell.layer.removeAllAnimations()
之后
cell.layoutIfNeeded()
线。
关于ios - 表格单元格约束无法正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43734759/