我试图弄清楚如何实现只发生一次(直到用户向上滑动以刷新表)的tableview单元动画,而不依赖于特定于VC的数组。我希望能够仅对数十个VC实施更改...但是我遇到了麻烦。
我认为我会遵循此线程中的建议:UITableviewcell animation only once。它说在自定义单元格声明中添加一个布尔值,并在willDisplay预动画中进行检查,但是我无法访问布尔值,因为该函数默认传入UItableviewcell。当我将其更改为自定义单元格类时,它将运行,但不再调用该函数。有什么想法吗?
func tableView(_ tableView: UITableView, willDisplay cell: CustomCell, forRowAt indexPath: IndexPath) {
//If transactionPage is set to -1, it's because we've reached the end of the transactions
if transactionPage != -1 && indexPath.row == (tableData.rows(inSection: indexPath.section).count) - 1 {
loadMoreTransactions()
}
if !cell.alreadyAnimated {
cell.alpha = 0
//Slide from bottom
let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200, 0)
cell.layer.transform = transform
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
})
cell.alreadyAnimated = true;
}
}
课程:
class CustomCell: UITableViewCell {
//MARK: Outlets
@IBOutlet private weak var descriptionLabel: UILabel!
@IBOutlet private weak var dateLabel: UILabel!
@IBOutlet private weak var amountLabel: UILabel!
//MARK: Properties
var alreadyAnimated = false;
var transaction: CustomTransaction! {
didSet {
descriptionLabel.text = transaction.desc
dateLabel.text = format.string(fromOptional: transaction.date)
amountLabel.text = transaction.getAmountString()
amountLabel.textColor = (transaction.amount < 0.0) ? .byuRed : .byuGreen
}
}
}
答案 0 :(得分:2)
您可以将UITableViewCell强制转换为CustomCell类:
func tableView(_: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let customCell = cell as? CustomCell {
// your code here
}
}
答案 1 :(得分:1)
我看到的问题是,在UITableViewDelegate
的协议声明中,您必须将..., willDisplay cell: CustomCell,...
更改为...willDisplay cell: UITableViewCell
,并将单元格强制转换为自定义类,以便能够访问其属性:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//If transactionPage is set to -1, it's because we've reached the end of the transactions
if transactionPage != -1 && indexPath.row == (tableData.rows(inSection: indexPath.section).count) - 1 {
loadMoreTransactions()
}
if let cell = cell as? CustomCell, !cell.alreadyAnimated {
cell.alpha = 0
//Slide from bottom
let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200, 0)
cell.layer.transform = transform
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
})
cell.alreadyAnimated = true;
}
}
由于方法的名称不同,系统会尝试在您的类中调用一个未声明的方法,这就是为什么您的代码未执行的原因。
答案 2 :(得分:1)
func tableView(_ tableView: UITableView, willDisplay cell: CustomCell, forRowAt indexPath: IndexPath) {...}
...这只是在声明新方法,您需要使用UITableViewDelegate
的方法和声明了参数的参数(因此,您需要保持UITableViewCell
类型的参数cell
)
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {...}
然后在inside方法中,您可以通过向下转换 cell
if let customCell = cell as? CustomCell {
...
}