๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[iOS/Swift] UICollectionViewFlowLayout ์•Œ์•„๋ณด๊ธฐ

by ํ‹ด๋”” 2022. 6. 6.
728x90
๋ฐ˜์‘ํ˜•

Mister Mister๋‹˜์˜ ์‚ฌ์ง„: https://www.pexels.com/ko-kr/photo/380782/

UICollectionViewFlowLayout

  • ๋ ˆ์ด์•„์›ƒ ์˜ค๋ธŒ์ ํŠธ
  • UICollectionViewController์˜ ์ด๋‹ˆ์…œ๋ผ์ด์ €์— ๋„˜๊ฒจ์ฃผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ collectionViewLayout ๊ฐ’.
init(collectionViewLayout: UICollectionViewLayout)
  • ๊ฐ ์„น์…˜์˜ ํ—ค๋”์™€ ํ‘ธํ„ฐ๋ฅผ ์˜ต์…”๋„ ๊ฐ’์œผ๋กœ ํ•˜์—ฌ ๊ทธ๋ฆฌ๋“œ๋ฅผ ๊ตฌ์„ฑํ•จ
@MainActor class UICollectionViewFlowLayout : UICollectionViewLayout
  • collection view layout ํƒ€์ž… 
  • flow layout์€ collection view์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์˜ค๋ธŒ์ ํŠธ์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜๋ฉฐ ๊ฐ ์„น์…˜ ์•ˆ์— ์•„์ดํ…œ(์…€), ํ—ค๋”, ํ‘ธํ„ฐ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ™œ์„ ํ•จ
  • ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์˜ค๋ธŒ์ ํŠธ๋Š” UICollectionViewDelegateFlowLayout ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ผ์•ผ ํ•จ
  • ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๊ฐ์ฒด๋Š” ๋ ˆ์ด์•„์›ƒ ์ •๋ณด๋ฅผ ๋‹ค์ด๋‚˜๋ฏนํ•˜๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ -> ๊ฐ๊ฐ์˜ ์…€ ํฌ๊ธฐ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

UICollectionViewLayout

  • collection view์˜ ๋ ˆ์ด์•„์›ƒ ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ abstract base class
@MainActor class UICollectionViewLayout : NSObject
  • ์…€, ํ‘ธํ„ฐ๋‚˜ ํ—ค๋”์™€ ๊ฐ™์€ supplementary views, collection view์˜ bounds ์•ˆ์— ์žˆ๋Š”  decoration views ๋ฐฐ์น˜๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉฐ ์ด ์ •๋ณด๋ฅผ collectionView์—๊ฒŒ ์•Œ๋ฆผ

์„œ๋ธŒ ํด๋ž˜์‹ฑ

  • layout ์˜ค๋ธŒ์ ํŠธ๋Š” ์ปฌ๋ ‰์…˜ ๋ทฐ ์•ˆ์— ์žˆ๋Š” ์•„์ดํ…œ์˜ ์œ„์น˜, ์‚ฌ์ด์ฆˆ, visual state๋ฅผ ์ •์˜
  • ๋ ˆ์ด์•„์›ƒ์„ ์œ„ํ•œ ๋ทฐ๋Š” ์ปฌ๋ ‰์…˜ ๋ทฐ์˜ ๋ฐ์ดํ„ฐ ์†Œ์Šค์— ์˜ํ•ด์„œ ์ƒ์„ฑ๋จ
  • ์…€ : ์‚ฌ์šฉ์ž์— ์˜ํ•ด selecting, dragging, reordering ๊ฐ€๋Šฅ. ์ปฌ๋ ‰์…˜ ๋ทฐ๋Š” single group of cells๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฑฐ๋‚˜ ์…€์„ ์—ฌ๋Ÿฌ ์„น์…˜์œผ๋กœ ๋ถ„๋ฅ˜ ํ•  ์ˆ˜ ์žˆ์Œ
  • supplementary views : (ํ—ค๋”์™€ ํ‘ธํ„ฐ๊ฐ€ ์—ฌ๊ธฐ์— ํ•ด๋‹น) ์œ ์ €์— ์˜ํ•ด์„œ selected ๋ถˆ๊ฐ€. ์˜ต์…”๋„.
  • decoration views : ๋ฑƒ์ง€์™€ ๊ฐ™์€ ๋น„์ฅฌ์–ผ ์ ์ธ ์š”์†Œ. ์ปฌ๋ ‰์…˜ ๋ทฐ์˜ ๋ฐ์ดํ„ฐ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์Œ. supplementary view์˜ ๋‹ค๋ฅธ ํƒ€์ž…์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ. ์˜ต์…”๋„.
  • ์ปฌ๋ ‰์…˜ ๋ทฐ๋Š” ์—ฌ๋Ÿฌ ํƒ€์ด๋ฐ์— elements๋ฅผ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ layout ์˜ค๋ธŒ์ ํŠธ์—๊ฒŒ ์š”์ฒญํ•จ
  • ๋ชจ๋“  ์…€๊ณผ ์Šคํฌ๋ฆฐ์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ทฐ๋Š” ๋ ˆ์ด์•„์›ƒ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ถ€ํ„ฐ ๋ฐ›์€ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ํ™”๋ฉด์— ๋ฐฐ์น˜ํ•˜๊ฒŒ ๋จ

Methods to Override

  • ์ปฌ๋ ‰์…˜ ๋ทฐ ์ปจํ…์ธ ์˜ ๋†’์ด์™€ ๋„“์ด
var collectionViewContentSize: CGSize { get }

 

  • ์ง€์ •๋œ ๋ฒ”์œ„์˜ ๋ชจ๋“  ์…€๊ณผ ๋ทฐ์— ๋Œ€ํ•œ layout attributes.
func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?

 

  • ์ง€์ •๋œ indexPath์— ์žˆ๋Š” ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ๋ ˆ์ด์•„์›ƒ ์ •๋ณด๋ฅผ ํ•ด๋‹น ์…€๊ณผ ํ•จ๊ป˜ ๋ฐ˜ํ™˜
func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?

 

  • ์ƒˆ๋กœ์šด bounds์˜ ๋ ˆ์ด์•„์›ƒ ์—…๋ฐ์ดํŠธ๊ฐ€ ํ•„์š”ํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ๋ ˆ์ด์•„์›ƒ ๊ฐœ์ฒด์—๊ฒŒ ์š”์ฒญ
func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool

 

 

 

UICollectionViewLayoutAttributes๋ž€

UICollectionViewLayout์˜ ์†์„ฑ๋“ค์€ UICollectionViewLayoutAttributes์˜ ์ธ์Šคํ„ด์Šค

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ ๋ฐ ๋„์„œ

https://developer.apple.com/documentation/uikit/uicollectionviewlayout

https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€