我在我的UITableView中添加了一个动画,但是当它第一次加载时,它不符合我设置的约束条件。他们是有点挤-高度和宽度的细胞。
动画位于UITableView、willDisplay cell的委托方法中,主体如下所示:
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次。这不是我想要的。
有谁知道我怎么才能修复这个错误吗?

提前感谢!
发布于 2017-05-02 10:05:17
试试这个:
在willDisplay委托方法中添加
cell.layer.removeAllAnimations()之后
cell.layoutIfNeeded()线路。
https://stackoverflow.com/questions/43734759
复制相似问题