[英]UIView.animation of transformation property doesn't work on UITableViewCell
我創建了一個視圖 controller 的項目,該視圖設置為初始視圖。 還有另一個視圖 controller 是 UITableViewController 的子類,它呈現 UITableViewCell 的子類,它有一個圖像視圖的出口。
初始視圖 controller 上的一個按鈕顯示了表格視圖 controller,它在“willDisplayCell”方法中設置了一個 animation 的圖像出口的轉換屬性。 當 tableview 顯示為卡片時它不起作用(modalPresentationStyle ==.automatic)並且它在 modalPresentationStyle ==.fullScreen 時起作用
這是 UIKit 錯誤嗎? 我的配置是 Xcode 12.4 (iOS 14.4)
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cel") as? CustomCell {
fatalError()
}
UIView.animate(withDuration: 0.2, delay: 0, options: [.repeat, .autoreverse]) {
cell.customImage.transform = .init(scaleX: 1.2, y: 1.2)
}
}
class CustomCell {
@IBOutlet private(set) var customImage: UIImageView!
}
不要在這里出列另一個單元格 - 您已經在方法簽名中獲得了單元格。 當您想從重用池中獲取單元格 object 以在tableView(_: cellForRow at:)
中返回時,出隊是為了。
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
UIView.animate(withDuration: 0.2, delay: 0, options: [.repeat, .autoreverse]) {
(cell as? CustomCell)?.customImage.transform = .init(scaleX: 1.2, y: 1.2)
}
}
首先,不要將新單元格出列 - dequeue
已經為您提供了對帶有這部分簽名的單元格的引用:
willDisplay cell: UITableViewCell
但是,您遇到了一些怪癖。 我不確定,但似乎當使用.automatic
呈現樣式時, UIView.animate(...)
調用正在呈現的 controller 上起作用,而不是呈現的 Z594C103F2C6E04CE03D8AB059F03。 可能是因為 UIKit 在不同的時間點生成表。
讓您的 animation 工作的一種方法是確保在視圖層次結構完全完成后發生調用。
試試這樣:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let c = cell as? CustomCell {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
UIView.animate(withDuration: 0.2, delay: 0, options: [.repeat, .autoreverse]) {
c.customImage.transform = .init(scaleX: 1.2, y: 1.2)
}
})
}
}
快速測試表明,如果表格視圖:
.automatic
,或.fullScreen
編輯-更多討論......
使用.automatic
和.fullScreen
呈現是有區別的。
特別是,使用.automatic
,當您關閉呈現的視圖 controller viewDidAppear()
時,呈現的 controller不會被調用。
因此,總體視圖 / controller 層次結構不是我們所期望的,並且(顯然)影響了我們在這里嘗試做的事情。
值得注意的是...如果我們不延遲UIView.animate(...)
調用,每個單元格中的customImage
會顯示為1.2
比例!
所以,似乎正在發生的事情是......當 UIKit 配置、渲染和顯示模態視圖 controller 時,該行被執行:
c.customImage.transform = .init(scaleX: 1.2, y: 1.2)
然后[.repeat, .autoreverse]
animation 將圖像視圖從1.2
縮放到1.2
並再次縮放(而不是從1.0
到1.2
)......所以我們看不到任何視覺變化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.