gpt4 book ai didi

ios - 当 collectionView 滚动时,如何使 iOS collectionView 的特定单元格淡出?

转载 作者:搜寻专家 更新时间:2023-10-30 22:59:07 27 4
gpt4 key购买 nike

我想让我的 UICollectionView 的所有右侧单元格在滚动时淡出,类似于 Apple 的消息应用程序,但不影响 collectionView 中其他单元格的颜色或透明度。有没有一种方法可以根据滚动位置调整 UICollectionViewCell 的透明度以实现该效果?

最佳答案

您可以对 Collection View 做很多有趣的事情。我喜欢继承 UICollectionViewFlowLayout。这是一个基于距中心的距离淡化 Collection View 的顶部和底部的示例。我可以将其修改为仅淡化边缘,但您应该在查看代码后理解它。

import UIKit

class FadingLayout: UICollectionViewFlowLayout,UICollectionViewDelegateFlowLayout {

//should be 0<fade<1
private let fadeFactor: CGFloat = 0.5
private let cellHeight : CGFloat = 60.0

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

init(scrollDirection:UICollectionViewScrollDirection) {
super.init()
self.scrollDirection = scrollDirection

}

override func prepare() {
setupLayout()
super.prepare()
}

func setupLayout() {
self.itemSize = CGSize(width: self.collectionView!.bounds.size.width,height:cellHeight)
self.minimumLineSpacing = 0
}

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

func scrollDirectionOver() -> UICollectionViewScrollDirection {
return UICollectionViewScrollDirection.vertical
}
//this will fade both top and bottom but can be adjusted
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributesSuper: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElements(in: rect) as [UICollectionViewLayoutAttributes]!
if let attributes = NSArray(array: attributesSuper, copyItems: true) as? [UICollectionViewLayoutAttributes]{
var visibleRect = CGRect()
visibleRect.origin = collectionView!.contentOffset
visibleRect.size = collectionView!.bounds.size
for attrs in attributes {
if attrs.frame.intersects(rect) {
let distance = visibleRect.midY - attrs.center.y
let normalizedDistance = abs(distance) / (visibleRect.height * fadeFactor)
let fade = 1 - normalizedDistance
attrs.alpha = fade
}
}
return attributes
}else{
return nil
}
}
//appear and disappear at 0
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItem(at: itemIndexPath)! as UICollectionViewLayoutAttributes
attributes.alpha = 0
return attributes
}

override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItem(at: itemIndexPath)! as UICollectionViewLayoutAttributes
attributes.alpha = 0
return attributes
}
}

在您的 Controller 中使用 Collection View 进行设置时,它看起来像这样。

let layout = FadingLayout(scrollDirection: .vertical)
collectionView.delegate = self
collectionView.dataSource = self
self.collectionView.setCollectionViewLayout(layout, animated: false)

如果我更了解用例,我可以告诉您如何修改它。

关于ios - 当 collectionView 滚动时,如何使 iOS collectionView 的特定单元格淡出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37941320/

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