13.
程序员文章站
2022-07-15 14:14:53
...
所有可见的cell
let cells = tableView.visibleCells
animate
for (index,cell) in cells.enumerated() {
cell.transform = CGAffineTransform(translationX: 0, y: tableHeight)
UIView.animate(withDuration: 1.0,
delay: 0.05*Double(index),
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0,
options: [],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: nil)
}
<li><h1>学习代码:<h1></li>
CustomTableViewCell.swift
import UIKit
class CustomTableViewCell: UITableViewCell {
let gradientLayer = CAGradientLayer()
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
gradientLayer.frame = self.bounds
let color1 = UIColor(white: 1.0, alpha: 0.2).cgColor
let color2 = UIColor(white: 1.0, alpha: 0.1).cgColor
let color3 = UIColor.clear.cgColor
let color4 = UIColor(white: 0.0, alpha: 0.05).cgColor
gradientLayer.colors = [color1,color2,color3,color4]
gradientLayer.locations = [0.0,0.04,0.95,0]
layer.insertSublayer(gradientLayer, at: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
gradientLayer.frame = self.bounds
}
}
ViewController.swift
import UIKit
let YHRect = UIScreen.main.bounds
let YHHeight = YHRect.size.height
let YHWidth = YHRect.size.width
class ViewController: UIViewController {
let tableData = ["Read 3 article on Medium", "Cleanup bedroom", "Go for a run", "Hit the gym", "Build another swift project", "Movement training", "Fix the layout problem of a client project", "Write the experience of #30daysSwift", "Inbox Zero", "Booking the ticket to Chengdu", "Test the Adobe Project Comet", "Hop on a call to mom", "Movement training", "Fix the layout problem of a client project", "Write the experience of #30daysSwift", "Inbox Zero", "Booking the ticket to Chengdu", "Test the Adobe Project Comet", "Hop on a call to mom"]
let tableView = UITableView(frame: YHRect, style: .plain)
let reuseIdentifier = "CustomCell"
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
override func viewWillAppear(_ animated: Bool) {
animateTable()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func setupView() {
tableView.separatorStyle = .none
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
view.addSubview(tableView)
}
func animateTable() {
tableView.reloadData()
//所有可见的cell
let cells = tableView.visibleCells
let tableHeight = tableView.bounds.size.height
for (index,cell) in cells.enumerated() {
cell.transform = CGAffineTransform(translationX: 0, y: tableHeight)
UIView.animate(withDuration: 1.0,
delay: 0.05*Double(index),
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0,
options: [],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: nil)
}
}
func colorforIndex(index: Int) -> UIColor {
let itemCount = tableData.count-1
let color = (CGFloat(index)/CGFloat(itemCount))*0.6
return UIColor(red: 1.0, green: color, blue: 0.0, alpha: 1.0)
}
}
extension ViewController:UITableViewDataSource, UITableViewDelegate {
//MARK: - DataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
cell.textLabel?.text = tableData[indexPath.row]
cell.textLabel?.textColor = .white
cell.textLabel?.backgroundColor = .clear
cell.textLabel?.font = UIFont(name: "Avenir Next", size: 18)
cell.selectionStyle = .gray
return cell
}
//MARK: - Delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = colorforIndex(index: indexPath.row)
}
}
上一篇: 13. 初始化列表
下一篇: 13.选择器案例:动态效果列表