CNLiveVocationalCertifiteCollectionViewLayout.swift
4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// CNLiveVocationalCertifiteCollectionViewLayout.swift
// metaCode
//
// Created by zx on 2024/7/25.
//
import UIKit
class CNLiveVocationalCertifiteCollectionViewLayout: UICollectionViewFlowLayout {
var cellWidth: CGFloat = 76.0
var cellHeight: CGFloat = 90.0
var bigCellWidth: CGFloat = 97.0
var bigCellHeight: CGFloat = 114.0
//
// override var collectionViewContentSize: CGSize {
// return CGSize(width: cellWidth * CGFloat(collectionView?.numberOfItems(inSection: 0) ?? 0), height: cellHeight)
// }
override func prepare() {
super.prepare()
// 这里可以添加额外的准备工作
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
// override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
// let collectionW = collectionView?.bounds.size.width ?? 0
// guard let superAttrs = super.layoutAttributesForElements(in: rect) else { return nil }
//
// var adjustedAttrs = [UICollectionViewLayoutAttributes]()
//
// // 遍历所有元素属性
// for attr in superAttrs {
// // 复制当前属性以避免直接修改原始属性
// let mutableAttr = attr.copy() as! UICollectionViewLayoutAttributes
//
// // 计算当前图片中心到集合视图最左侧的距离
// let offsetX = mutableAttr.center.x - (collectionView?.contentOffset.x ?? 0)
//
// // 检查是否是左侧第一个图片,并且距离小于或等于100点
// if offsetX <= 100 && offsetX >= 0 { // 确保图片在集合视图内
// // 计算放大比例(这里假设最大放大到1.3倍,可以根据需要调整)
// let scaleFactor = 1 - (offsetX / 100.0) * (1 - 1.3)
// let scale = max(scaleFactor, 1.0) // 确保不会缩小
// mutableAttr.transform = CGAffineTransform(scaleX: scale, y: scale)
// print("1111scale:\(scale)")
//
// } else {
// // 如果不是左侧第一个图片或距离太远,则不放大
// mutableAttr.transform = CGAffineTransform.identity
// }
// print("1111offsetX:\(offsetX)")
//
//
// adjustedAttrs.append(mutableAttr)
// }
//
// return adjustedAttrs
// }
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let collectionW = collectionView?.bounds.size.width ?? 0
guard let attrs = super.layoutAttributesForElements( in: collectionView?.bounds ?? .zero) else { return nil }
for attr in attrs {
let margin = abs((attr.center.x - (collectionView?.contentOffset.x ?? 0)))
let bigScale:CGFloat = 114.0/90.0
var scale:CGFloat = 1.0
let maxOffX:CGFloat = 153
let minOffX:CGFloat = 53
if margin >= minOffX && margin <= maxOffX{
scale = 1+(bigScale-1)*((maxOffX-margin)/100.0)
}else{
scale = 1.0
}
attr.transform = CGAffineTransform(scaleX: scale, y: scale)
}
return attrs
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let collectionW = collectionView?.bounds.size.width ?? 0
var targetP = proposedContentOffset
guard let attrs = super.layoutAttributesForElements(in: CGRect(x: targetP.x, y: 0, width: collectionW, height: CGFloat.greatestFiniteMagnitude)) else {
return targetP
}
var minSpacing: CGFloat = CGFloat.greatestFiniteMagnitude
for attr in attrs {
let centerOffsetX = attr.center.x - targetP.x - collectionW * 0.5
if abs(centerOffsetX) < abs(minSpacing) {
minSpacing = centerOffsetX
}
}
targetP.x += minSpacing
return targetP
}
}