基于中描述的問題

如你看到的,
- 展開影片效果很好!
- 對于折疊影片,內部
UIView會立即消失,沒有任何影片。
你有什么想法,為什么崩潰時沒有隱藏影片?
這是執行折疊/展開的代碼。
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return shops.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let collectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionViewCell else {
fatalError()
}
let shop = shops[indexPath.item]
collectionViewCell.title.text = shop.title
collectionViewCell._description.text = shop.description
if isExpanded[indexPath.item] {
collectionViewCell.innerView.isHidden = false
} else {
collectionViewCell.innerView.isHidden = true
}
return collectionViewCell
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
for i in (0..<isExpanded.count) {
if i == indexPath.item {
// ensure always visible
isExpanded[i] = true
} else {
// set all other rows to false
isExpanded[i] = false
}
if let c = collectionView.cellForItem(at: IndexPath(item: i, section: 0)) as? CollectionViewCell {
c.innerView.isHidden = !isExpanded[i]
}
}
collectionView.performBatchUpdates(nil, completion: nil)
}
}
到目前為止我嘗試過的是
- 使用零高度約束來激活/停用,替換
isHidden - 采用
UIView.animate
但是,內部UIView會立即消失,沒有任何高度收縮影片。
你知道我該如何解決這個問題嗎?謝謝。
這是說明問題的代碼 - https://github.com/yccheok/shop-dialog/tree/c399bca163096ad27de7de866af5d2de370a8afb
uj5u.com熱心網友回復:
正如我在對另一個問題的評論中提到的那樣,我們很少找到“一刀切”的解決方案。
.isHidden在堆疊視圖的排列子視圖上設定時,不是與默認行為作斗爭,而是一種不同的方法。
使用兩個底部約束:
- 一個來自底部的“頂部/始終可見”的 UI 元素
- “顯示/隱藏”元素底部的一個(它們的容器視圖)
將第二個約束的 設定為 750 ( .priority) 。.defaultHigh
當您希望單元格“折疊”時,將第一個約束設定.priority為751 ( .defaultHigh 1)。
當您希望單元格被“擴展”時,將第一個約束的 設定.priority為749 ( .defaultHigh - 1)。
要影片展開/折疊效果,請包裹performBatchUpdates在一個UIView.animate塊中。
我在這里分叉了您的 GitHub 存盤庫 - https://github.com/DonMag/shop-dialog - 并將此方法添加為“V2”,以便您可以檢查它并查看差異。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459073.html
標籤:IOS 迅速 动画片 uicollectionview uistackview
上一篇:在顫動中點擊影片時如何切換影片
