UICollectionView布局cell的三種方式


UICollectionViewFlowLayout里面:

1 // 方法一
2 - (void)prepareLayout{}
3 // 方法二:可以修改寬度,不能修改高度
4 - (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect

貼出具體代碼,注意方法二,要和一個帶bool返回值的方法一塊用:

 1 class CoverFlowLayout: UICollectionViewFlowLayout {
 2 
 3     // MARK: - 准備布局
 4     override func prepare() {
 5         super.prepare()
 6         
 7         scrollDirection = .horizontal
 8         
 9         let itemH = (collectionView?.bounds.size.height ?? 0) * 0.8
10         let itemW = itemH
11         itemSize = CGSize(width: itemW, height: itemH)
12         
13         minimumLineSpacing = 0
14     }
15     
16     // MARK: - 只要顯示的區域發生變化,就重新計算布局
17     // true - 只要顯示的區域發生改變,就讓布局失效; -> 重新計算布局,執行下面的方法
18     override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
19         return true
20     }
21     
22     // MARK: - 布局某表Rect中的所有cell
23     override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
24         
25         // 先獲取系統布局好的結果
26         let oldAttsArray = super.layoutAttributesForElements(in: rect)!
27         
28         // 創建新集合
29         var tempArray = [UICollectionViewLayoutAttributes]()
30         
31         // 遍歷集合,進行修改
32         for oldAtt in oldAttsArray{
33         
34             // 這里不能直接修改,需先copy(否則控制台會輸出錯誤)
35             let newAtt = oldAtt.copy() as! UICollectionViewLayoutAttributes
36             
37             // 屏幕中線位置
38             let screenCenterX = (collectionView?.bounds.size.width ?? 0) * 0.5 + (collectionView?.contentOffset.x ?? 0)
39             
40             // cell的中線
41             let itemCenterX = newAtt.center.x
42             
43             // 計算距離
44             let distance = screenCenterX - itemCenterX
45             
46             // 將距離轉換成縮放比例
47             let scale = 1 - abs(distance) / (collectionView?.bounds.size.width ?? 1)
48             
49             let transform = CATransform3DIdentity
50             newAtt.transform3D = CATransform3DScale(transform, scale, scale, 1)
51                 
52             tempArray.append(newAtt)
53         }
54         
55         return tempArray
56     }
57     
58 }

 

 

協議方法 UICollectionViewDelegateFlowLayout:

 1 // 方法三:返回cell的size
 2 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
 3     if (indexPath.section == 0) {  //第0組
 4         return CGSizeMake(collectionView.bounds.size.width, 100);
 5     }
 6    
 7     if (indexPath.section == 1 && indexPath.item == 0) {  //第一組的第0個
 8         return CGSizeMake(collectionView.bounds.size.width, 64);
 9     }
10    
11     CGFloat width = (collectionView.bounds.size.width - 1) / 2;
12     CGFloat height = 64;
13     return CGSizeMake(width, height);
14 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM