Open hcn1519 opened 4 years ago
Layout을 직접 구현해야 하는 경우
스크롤 가능한 content 영역을 설정한다. 각각의 cell, view를 적절히 배치하기 위한 attributes 객체를 제공한다.
collectionView는 layout 정보가 필요할 때, lyaout 객체에 그 정보를 요청한다.
정보 요청 시점
invalidateLayout()
메소드가 호출되었을 때prepare()
최초의 layout 정보(모든 Cell과 View의 frame 정보)에 대해서 계산한다. 해당 메소드가 호출 완료되는 시점까지 content 영역에 대한 사이즈를 제공할 수 있어야 한다.
collectionViewContentSize
초기 계산을 기반으로 한 전체 content 영역의 사이즈를 리턴한다. collectionView는 contentSize를 기반으로 스크롤 가능한 영역을 결정하므로, 이 값을 적절히 설정해야 원하는 스크롤 영역을 결정할 수 있다.
layoutAttributesForElementsInRect(:)
현재의 스크롤 위치를 기반으로 collectionView는 layoutAttributesForElementsInRect(:)
을 통해 특정 rect 영역 안에 있는 Cell과 View를 요청한다.
UICollectionViewLayoutAttributes
타입의 인스턴스가 각 Item별로 Layout에 대한 정보를 가진다. 생성해야 하는 Items의 개수가 수천개가 되지 않을 때에는 UICollectionViewLayoutAttributes
를 prepare()
단계에서 만드는 것이 가장 좋다. UICollectionViewLayoutAttributes
객체는 캐싱될 수 있고, 캐싱된 객체를 리턴하는 것이 성능적으로 가장 좋다.
다만, 필요에 따라서(prepare()에서 지나치게 많은 Item을 처리해야 할 경우, lazy하게 layout을 계산하는 것이 더 좋을 경우 등)
UICollectionViewLayoutAttributes
를 생성할 때에는 최소한 frame에 대한 처리가 있어야 한다.
layoutAttributesForElementsInRect(:)
의 목적은 collectionView가 특정 Rect 영역에 대해서 정보를 요청할 때, 이 영역과 겹치는 모든 Cell과 View의 LayoutAttributes를 제공하는 것이다. CollectionView는 현재 화면에 보이는 content 영역에 대한 Rect를 요청한다.
layoutAttributesForElementsInRect(:)
에서 파라미터로 넘어오는 rect와 겹치는 것들을 추출한다.layoutAttributesForElementsInRect(:)
에서 매번 새로운 layoutAttributes를 매번 생성하는 것은 비용이 큰 작업이다. 되도록이면 캐싱하는 것이 좋다.
collectionView는 각각의 item들의 layoutAttributes에 대해서 주기적으로 layout 객체에 요청한다. 예를 들어서 collectionView가 insert/delete 동작을 수행할 때, 각각의 item에 대한 정보를 요청한다. 이 때, layout 객체는 각각의 Cell과 View에 대해서 적절한 정보를 준비하고 있어야 한다.
layoutAttributesForItemAtIndexPath(:)
를 오버라이딩하여
이번 글에서는 UICollectionView의 Custom Layout을 그리는 과정과 방법에 대해 알아보고자 합니다.
Hello
UICollectionView는 UICollectionViewDataSource을 통해
FlowLayout 객체를 통해서 화면에 Cell을 그립니다.
Layout을 직접 구현해야 하는 경우
스크롤 가능한 content 영역을 설정한다. 각각의 cell, view를 적절히 배치하기 위한 attributes 객체를 제공한다.
collectionView는 layout 정보가 필요할 때, lyaout 객체에 그 정보를 요청한다.
정보 요청 시점
invalidateLayout()
메소드가 호출되었을 때prepare()
최초의 layout 정보(모든 Cell과 View의 frame 정보)에 대해서 계산한다. 해당 메소드가 호출 완료되는 시점까지 content 영역에 대한 사이즈를 제공할 수 있어야 한다.
collectionViewContentSize
초기 계산을 기반으로 한 전체 content 영역의 사이즈를 리턴한다. collectionView는 contentSize를 기반으로 스크롤 가능한 영역을 결정하므로, 이 값을 적절히 설정해야 원하는 스크롤 영역을 결정할 수 있다.
layoutAttributesForElementsInRect(:)
현재의 스크롤 위치를 기반으로 collectionView는 layoutAttributesForElementsInRect(:)
을 통해 특정 rect 영역 안에 있는 Cell과 View를 요청한다.
UICollectionViewLayoutAttributes
타입의 인스턴스가 각 Item별로 Layout에 대한 정보를 가진다. 생성해야 하는 Items의 개수가 수천개가 되지 않을 때에는 UICollectionViewLayoutAttributes
를 prepare()
단계에서 만드는 것이 가장 좋다. UICollectionViewLayoutAttributes
객체는 캐싱될 수 있고, 캐싱된 객체를 리턴하는 것이 성능적으로 가장 좋다.
다만, 필요에 따라서(prepare()에서 지나치게 많은 Item을 처리해야 할 경우, lazy하게 layout을 계산하는 것이 더 좋을 경우 등)
UICollectionViewLayoutAttributes
를 생성할 때에는 최소한 frame에 대한 처리가 있어야 한다.
layoutAttributesForElementsInRect(:)
의 목적은 collectionView가 특정 Rect 영역에 대해서 정보를 요청할 때, 이 영역과 겹치는 모든 Cell과 View의 LayoutAttributes를 제공하는 것이다. CollectionView는 현재 화면에 보이는 content 영역에 대한 Rect를 요청한다.
layoutAttributesForElementsInRect(:)
에서 파라미터로 넘어오는 rect와 겹치는 것들을 추출한다.layoutAttributesForElementsInRect(:)
에서 매번 새로운 layoutAttributes를 매번 생성하는 것은 비용이 큰 작업이다. 되도록이면 캐싱하는 것이 좋다.
collectionView는 주기적으로 각각의 item들의 layoutAttributes를 layout 객체에 요청한다. 예를 들어서 collectionView는 insert/delete 동작을 수행할 때, 각각의 item에 대한 layoutAttributes를 layout에 요청한다. 이 때, layout 객체는 반드시 각각의 Cell과 View에 대해서 적절한 정보를 준비하고 있어야 한다.
collectionView는 layoutAttributesForItemAtIndexPath(:)
메소드를 통해 필요한 item의 layoutAttributes를 요청하므로, 해당 메소드를 오버라이딩하여 적절한 UICollectionViewLayoutAttributes
객체를 리턴해주어야 한다.
https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCustomLayouts/CreatingCustomLayouts.html#//apple_ref/doc/uid/TP40012334-CH5-SW1