我是Swift的新手,作为一个小项目,我想创建类似“选择自己的冒险游戏”之类的东西。
我尝试通过TableView实现此目的,其中每个单元格都包含数组中的故事情节。
使用我当前在App Start上的代码,每个单元格都会立即生成并显示数组的字符串-正确,但我希望随时间生成每个单元格。
可选:如果可能的话,最好在每个Char字符中按延迟显示String,这样字符以0.5秒的延迟一一显示-在完成第一个String / Cell之后,它应该生成第二个Cell并写出第二个String。
您对我如何实现此目标有何建议?
谢谢!
这是我当前的代码,因为我不知道如何管理它,所以不包含任何有关延迟的功能:)
import UIKit
class ViewController: UIViewController {
var storyLines = ["Test Cell 1 and so on first cell of many","second cell bla","aaaand third cell sup"]
var actualTables = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
}
@IBOutlet weak var tableView: UITableView!
}
extension ViewController : UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storyLines.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TxtLine", for: indexPath)
cell.textLabel?.text = storyLines[indexPath.row]
cell.textLabel?.numberOfLines = 0;
cell.textLabel?.lineBreakMode = .byWordWrapping
return cell
}
}
答案 0 :(得分:0)
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614883-tableview
在 uitableviewdelegate 中有一个名为 tableView(_:willDisplay:forRowAt:)的实例方法。您可以覆盖该方法并使表格视图单元具有动画效果。您可以参考以下代码作为示例。
ps u
别忘了将viewcontroller设置为tableview的委托。
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.transform = CGAffineTransform(translationX: tableView.bounds.width, y: 0)
UIView.animate(
withDuration: 0.5,
delay: 0.05 * Double(indexPath.row),
options: [.curveEaseInOut],
animations: {
cell.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
}