gpt4 book ai didi

ios - 将动画应用于 UIKit 类的每个实例?

转载 作者:行者123 更新时间:2023-11-28 13:54:03 35 4
gpt4 key购买 nike

假设我有一些简单的 tableview 动画代码。有没有办法将其应用于我项目中的每个 TableView ?我当前的项目非常大,每个文件夹都有单独的 VC 和 Storyboard。有什么方法可以普遍应用更改?

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 arrIndexPath.contains(indexPath) == false {
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
})

arrIndexPath.append(indexPath)
}
}

最佳答案

协议(protocol)

第一个可能的解决方案是创建协议(protocol)。创建协议(protocol),然后扩展它并声明为单元格设置动画的方法

protocol CellAnimating where Self: UITableViewDelegate {}

extension CellAnimating {

func animateCell(_ cell: UITableViewCell) {
cell.alpha = 0

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
})
}

}

然后查看您想要使用它的 Controller ,只需实现此协议(protocol)并在 willDisplay 中调用此方法

class ViewController: UIViewController, UITableViewDelegate {
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
...
animateCell(cell)
...
}
}

extension ViewController: CellAnimating {}

子类化 View Controller

我想到的第二个选项是您可以创建 UIViewController 的子类,并在委托(delegate)方法 willDisplay 中声明一些内容

class ViewController: UIViewController, UITableViewDelegate {
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
...
}
}

然后只需将要使用它的每个 View Controller 设置为 ViewController

的子类

关于ios - 将动画应用于 UIKit 类的每个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116856/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com