-
(Swift) visibleItemsInvalidationHandleriOS💖 2024. 9. 25. 17:43
(문제 배경)
Collection View의 compositional layout와 diffable data source 을 이용하여, 카드형식의 layout을 구현하고 있었다.
해당 layout에 page control을 사용하기 위해, layout 의 현재 위치(좌표) / offset 이 필요했다.
아래의 Handler 로 해결됨!
#1. visibleItemsInvalidationHandler
: 각 layout이 변경이 있을 때 마다, layout 관련하여 변경된 속성들(데이터)를 확인할 수 있는 Handler 이다.
(Apple Developer Documentation)
A closure called before each layout cycle to allow modification of the items in the section immediately before they’re displayed.
>> visibleItemsInvalidationHandler 은, 클로저이다.
Section의 Item들이 표시되기 직전에 수정할 수 있도록 각 layout 사이클 전에 불리는 클로저이다.
var visibleItemsInvalidationHandler: NSCollectionLayoutSectionVisibleItemsInvalidationHandler? { get set }
#2. NSCollectionLayoutSectionVisibleItemsInvalidationHandler
그렇다면, NSCollectionLayoutSectionVisibleItemsInvalidationHandler 은 뭐지?
아! 별칭으로 지정된 이름이었구나
그 속을 더 파헤쳐 보자!
typealias NSCollectionLayoutSectionVisibleItemsInvalidationHandler = ([any NSCollectionLayoutVisibleItem], CGPoint, any NSCollectionLayoutEnvironment) -> Void아, Item ( [any NSCollectionLayoutVisibleItem] ) , 현재 위치 (CGPoint) , 그리고 NSCollectionLayoutEnvironment 을 알 수 있게 해주는 아이구나!
...
NSCollectionLayoutEnvironment는 뭐지?
#3. NSCollectionLayoutEnvironment
CollectionLayout의 어떤 환경을 알려준다는 거지?
(Apple Developer Documentation)
A protocol used to provide information about the layout’s container and environment traits, such as size classes and display scale factor.
>> 현재 layout 의 container 등등..
의 environment 데이터를 갖고있음

#4. 사용해보기private func layout() -> UICollectionViewCompositionalLayout { let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.8), heightDimension: .absolute(200)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group) // section에 구애받지 않고, horizental 로 layout 해 section.orthogonalScrollingBehavior = .groupPagingCentered section.interGroupSpacing = 20 section.visibleItemsInvalidationHandler = { (item, offset, env) in let index = Int((offset.x / (env.container.contentSize,width * 0.9)).rounded(.up)) //print(index) self.pageControl.currentPage = index } let layout = UICollectionViewCompositionalLayout(section: section) return layout }# 1 visibleItemsInvalidationHandler
visibleItemsInvalidationHandler | Apple Developer Documentation
A closure called before each layout cycle to allow modification of the items in the section immediately before they’re displayed.
developer.apple.com
# 2 NSCollectionLayoutSectionVisibleItemsInvalidationHandler
NSCollectionLayoutSectionVisibleItemsInvalidationHandler | Apple Developer Documentation
A closure called before each layout cycle to allow modification of items in a section immediately before they’re displayed.
developer.apple.com
# 3 NSCollectionLayoutEnvironment
https://developer.apple.com/documentation/uikit/nscollectionlayoutenvironment
NSCollectionLayoutEnvironment | Apple Developer Documentation
A protocol used to provide information about the layout’s container and environment traits, such as size classes and display scale factor.
developer.apple.com
'iOS💖' 카테고리의 다른 글
(Swift) NSCollectionLayoutGroup. vertical / horizontal method 종류 (2) 2024.09.26 (Swift) Collection Reusable View 사용 (0) 2024.09.26 (Swift) UISearchController로 Search Bar 구현하기 (0) 2024.09.12 (Swift) sorted() (2) 2024.09.11 (Swift) DateFormatter (1) 2024.09.11