https://github.com/hyun99999/pinteresttutorial-ios
๐ฅบPinterest Layout Tutorial
https://github.com/hyun99999/pinteresttutorial-ios
Last synced: about 2 months ago
JSON representation
๐ฅบPinterest Layout Tutorial
- Host: GitHub
- URL: https://github.com/hyun99999/pinteresttutorial-ios
- Owner: hyun99999
- Created: 2021-06-20T12:52:48.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-23T05:57:24.000Z (almost 4 years ago)
- Last Synced: 2025-03-25T11:49:22.410Z (3 months ago)
- Language: Swift
- Size: 36.8 MB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PinterestTutorial-iOS
๐ฅบPinterest Layout Tutorial์ด๋ฏธ์ง ํฌ๊ธฐ์ ๋ฐ๋ผ์ ๋์ ์ผ๋ก ์ ์ ๋ ์ด์์์ ์ค์ ํ๋ ํํฐ๋ ์คํธ ๋ ์ด์์ ๊ตฌํํด ๋ณด์๋ค.
### ์์ฑ
### ์ฝ๋
- UICollectionViewDelegateFlowLayout ์ ์๋ธํด๋์ค์ธ PinterestLayout ์์ฑ.
```swift
// PinterestLayout ์์ ๊ฐ ์ด๋ฏธ์ง ๋์ด๋ฅผ ์ ์ ์๋๋ก Delegate ์์ฑ.
protocol PinterestLayoutDelegate: AnyObject {
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat
}class PinterestLayout: UICollectionViewFlowLayout {
// delegate๋ก ViewController ๋ฅผ ๋ํ๋ธ๋ค.
weak var delegate: PinterestLayoutDelegate?
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
guard let collectionView = collectionView else {
return 0
}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}
// 1. ์ฝ๋ ์ ๋ทฐ์ ์ฝํ ์ธ ์ฌ์ด์ฆ๋ฅผ ์ง์ ํฉ๋๋ค.
override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
// ๋ค์ ๋ ์ด์์์ ๊ณ์ฐํ ํ์๊ฐ ์๋๋ก ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅํฉ๋๋ค.
private var cache: [UICollectionViewLayoutAttributes] = []
// 2. ์ฝ๋ ์ ๋ทฐ๊ฐ ์ฒ์ ์ด๊ธฐํ๋๊ฑฐ๋ ๋ทฐ๊ฐ ๋ณ๊ฒฝ๋ ๋ ์คํ๋ฉ๋๋ค. ์ด ๋ฉ์๋์์ ๋ ์ด์์์
// ๋ฏธ๋ฆฌ ๊ณ์ฐํ์ฌ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฌํ๊ณ , ํ์ํ ๋๋ง๋ค ํจ์จ์ ์ผ๋ก ์ ๊ทผํ ์ ์๋๋ก ๊ตฌํํด์ผ ํฉ๋๋ค.
override func prepare() {
guard let collectionView = collectionView, cache.isEmpty else { return }
let numberOfColumns: Int = 2 // ํ ํ์ ์์ดํ ๊ฐฏ์
let cellPadding: CGFloat = 5
let cellWidth: CGFloat = contentWidth / CGFloat(numberOfColumns)
let xOffSet: [CGFloat] = [0, cellWidth] // cell ์ x ์์น๋ฅผ ๋ํ๋ด๋ ๋ฐฐ์ด
var yOffSet: [CGFloat] = .init(repeating: 0, count: numberOfColumns) // // cell ์ y ์์น๋ฅผ ๋ํ๋ด๋ ๋ฐฐ์ด
var column: Int = 0 // ํ์ฌ ํ์ ์์น
for item in 0.. yOffSet[1] ? 1 : 0
}
}
// 3. ๋ชจ๋ ์ ๊ณผ ๋ณด์ถฉ ๋ทฐ์ ๋ ์ด์์ ์ ๋ณด๋ฅผ ๋ฆฌํดํฉ๋๋ค. ํ๋ฉด ํ์ ์์ญ ๊ธฐ๋ฐ(Rect)์ ์์ฒญ์ด ๋ค์ด์ฌ ๋ ์ฌ์ฉํฉ๋๋ค.
override func layoutAttributesForElements(in rect: CGRect)
-> [UICollectionViewLayoutAttributes]? {
var visibleLayoutAttributes: [UICollectionViewLayoutAttributes] = []
for attributes in cache {
if attributes.frame.intersects(rect) { // ์ frame ๊ณผ ์์ฒญ Rect ๊ฐ ๊ต์ฐจํ๋ค๋ฉด, ๋ฆฌํด ๊ฐ์ ์ถ๊ฐํฉ๋๋ค.
visibleLayoutAttributes.append(attributes)
}
}
return visibleLayoutAttributes
}
// 4. ๋ชจ๋ ์ ์ ๋ ์ด์์ ์ ๋ณด๋ฅผ ๋ฆฌํดํฉ๋๋ค. IndexPath ๋ก ์์ฒญ์ด ๋ค์ด์ฌ ๋ ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
override func layoutAttributesForItem(at indexPath: IndexPath)
-> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
}
```
- MainVC ์์ PinterestLayoutDelegate ๋ฅผ ์ฑํ.```swift
// MARK: - UICollectionViewDelegateFlowLayout
extension MainVC: PinterestLayoutDelegate {
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat {
let cellWidth: CGFloat = (view.bounds.width - 4) / 2 // ์ ๊ฐ๋ก ํฌ๊ธฐ
let imageHeight = imageList[indexPath.item].image.size.height
let imageWidth = imageList[indexPath.item].image.size.width
// ์ด๋ฏธ์ง ๋น์จ
let imageRatio = imageHeight/imageWidth
return imageRatio * cellWidth
}
}
```### ์ถ์ฒ
์ถ์ฒใ ฃ[Swift. UICollectionView Pinterest ๋ ์ด์์](https://devmjun.github.io/archive/CollectionView-Pinterest)