gpt4 book ai didi

ios - 如何在 iOS Swift 4 中为 UITableView 的部分设置阴影?

转载 作者:行者123 更新时间:2023-12-03 09:16:41 24 4
gpt4 key购买 nike

我想在 iOS Swift 中的每个部分(单元组)周围设置阴影。
像这样:
enter image description here

最佳答案

使用 UICollectionView 轻松实现此目的,
按子类 UICollectionViewFlowLayout DecorationViewUITableViewCell进入 UICollectionViewCell ,
使用装饰 View 实现截面部分的投影


class DecorationFlow: UICollectionViewFlowLayout {

private var cache = [IndexPath: UICollectionViewLayoutAttributes]()

override func prepare() {
super.prepare()
register(HistoDecorationV.self, forDecorationViewOfKind: HistoDecorationV.id)
let originX: CGFloat = 15
let widH: CGFloat = UI.std.width - originX * 2
guard let collect = collectionView else{
return
}
let sections = collect.numberOfSections
var originY: CGFloat = 0
for sect in 0..<sections{
let sectionFirst = IndexPath(item: 0, section: sect)
let attributes = UICollectionViewLayoutAttributes(forDecorationViewOfKind: HistoDecorationV.id, with: sectionFirst)
let itemCount = collect.numberOfItems(inSection: sect)
originY = originY + 80
let h: CGFloat = 50 * CGFloat(itemCount)
attributes.frame = CGRect(x: originX, y: originY, width: widH, height: h)
originY = originY + h + 15
cache[sectionFirst] = attributes
}
}



override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}


private func prepareCache() {
cache.removeAll(keepingCapacity: true)
cache = [IndexPath: UICollectionViewLayoutAttributes]()
}


override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return cache[indexPath]

}


override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var array = super.layoutAttributesForElements(in: rect)
guard let collection = collectionView, collection.numberOfSections > 0 else{
return array
}
var z = 0
array?.forEach({
$0.zIndex = z + 100
z += 1
})
z = 0
for (_, attributes) in cache {

if attributes.frame.intersects(rect){
attributes.zIndex = z + 10
array?.append(attributes)
}
z += 1
}
return array
}
}
more code in github

关于ios - 如何在 iOS Swift 4 中为 UITableView 的部分设置阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47119417/

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