@@ -29,26 +29,34 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
2929 textColor: . gray600,
3030 font: . pickFont( . body2)
3131 )
32- private let floorSegmentedControl = ClassroomSegmentedControl ( items: [
33- " 1층 " , " 2층 " , " 3층 " , " 4층 " , " 5층 "
34- ] ) . then {
32+ private let floorSegmentedControl = ClassroomSegmentedControl ( items:
33+ ClassroomData . shared. allFloors. enumerated ( ) . map { index, _ in
34+ " \( index + 1 ) 층 "
35+ }
36+ ) . then {
3537 $0. selectedSegmentIndex = 0
3638 }
37- private lazy var collectionViewFlowLayout = LeftAlignedCollectionViewFlowLayout ( ) . then {
38- $0. estimatedItemSize = UICollectionViewFlowLayout . automaticSize
39- $0. minimumLineSpacing = 15
40- $0. minimumInteritemSpacing = 16
39+ private lazy var backgroundcollectionViewFlowLayout = UICollectionViewFlowLayout ( ) . then {
40+ $0. scrollDirection = . horizontal
41+ $0. itemSize = . init(
42+ width: self . view. frame. width - 48 ,
43+ height: self . view. frame. height * 0.7
44+ )
45+ $0. minimumLineSpacing = 0
46+ $0. minimumInteritemSpacing = 0
4147 }
42- private lazy var classroomCollectionView = UICollectionView (
48+ private lazy var backgroundCollectionView = UICollectionView (
4349 frame: . zero,
44- collectionViewLayout: collectionViewFlowLayout
50+ collectionViewLayout: backgroundcollectionViewFlowLayout
4551 ) . then {
4652 $0. backgroundColor = . background
4753 $0. register (
48- ClassroomCollectionViewCell . self,
49- forCellWithReuseIdentifier: ClassroomCollectionViewCell . identifier
54+ ClassroomBackgroundCell . self,
55+ forCellWithReuseIdentifier: ClassroomBackgroundCell . identifier
5056 )
51- $0. contentInsetAdjustmentBehavior = . always
57+ $0. showsHorizontalScrollIndicator = false
58+ $0. showsVerticalScrollIndicator = false
59+ $0. isPagingEnabled = true
5260 $0. bounces = false
5361 }
5462 private let nextButton = PiCKButton ( buttonText: " 다음 " )
@@ -77,42 +85,66 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
7785 } . disposed ( by: disposeBag)
7886
7987 floorSegmentedControl. rx. selectedSegmentIndex
80- . map { [ weak self] index -> [ String ] in
81- self ? . selectedSegemetedControlIndex. accept ( index + 1 )
82- self ? . classroomText. accept ( " " )
88+ . withUnretained ( self )
89+ . map { owner, index -> [ String ] in
90+ owner. selectedSegemetedControlIndex. accept ( index + 1 )
91+ owner. classroomText. accept ( " " )
92+ let itemCount = owner. backgroundCollectionView. numberOfItems ( inSection: 0 )
93+
94+ guard itemCount > 0 , index < itemCount else {
95+ return owner. classroomData. firstFloor
96+ }
97+
98+ owner. backgroundCollectionView. scrollToItem (
99+ at: IndexPath ( item: index, section: 0 ) ,
100+ at: . centeredHorizontally,
101+ animated: false
102+ )
103+
83104 switch index {
84105 case 0 :
85- return self ? . classroomData. firstFloor ?? [ ]
106+ return owner . classroomData. firstFloor
86107 case 1 :
87- return self ? . classroomData. secondFloor ?? [ ]
108+ return owner . classroomData. secondFloor
88109 case 2 :
89- return self ? . classroomData. thirdFloor ?? [ ]
110+ return owner . classroomData. thirdFloor
90111 case 3 :
91- return self ? . classroomData. fourthFloor ?? [ ]
112+ return owner . classroomData. fourthFloor
92113 case 4 :
93- return self ? . classroomData. fifthFloor ?? [ ]
114+ return owner . classroomData. fifthFloor
94115 default :
95- return self ? . classroomData. firstFloor ?? [ ]
116+ return owner . classroomData. firstFloor
96117 }
97118 }
98119 . bind ( to: currentFloorClassroomArray)
99120 . disposed ( by: disposeBag)
100121
101- currentFloorClassroomArray
102- . bind ( to: classroomCollectionView . rx. items (
103- cellIdentifier: ClassroomCollectionViewCell . identifier,
104- cellType: ClassroomCollectionViewCell . self
122+ Observable . just ( classroomData . allFloors )
123+ . bind ( to: backgroundCollectionView . rx. items (
124+ cellIdentifier: ClassroomBackgroundCell . identifier,
125+ cellType: ClassroomBackgroundCell . self
105126 ) ) { _, item, cell in
106- cell. setup ( classroom: item)
127+ cell. setup ( data: item)
128+ cell. didSelectClassroom = { [ weak self] classroom in
129+ self ? . classroomText. accept ( classroom)
130+ }
107131 } . disposed ( by: disposeBag)
108132
109- classroomCollectionView . rx. itemSelected
133+ backgroundCollectionView . rx. didScroll
110134 . withUnretained ( self )
111- . bind { owner, index in
112- owner. classroomText. accept (
113- owner. currentFloorClassroomArray. value [ index. row]
114- )
115- } . disposed ( by: disposeBag)
135+ . bind { owner, _ in
136+ guard let layout = owner. backgroundCollectionView. collectionViewLayout as? UICollectionViewFlowLayout else { return }
137+
138+ let cellWidth = layout. itemSize. width
139+ let spacing = layout. minimumLineSpacing
140+ let totalCellWidth = cellWidth + spacing
141+
142+ let offset = owner. backgroundCollectionView. contentOffset. x + owner. backgroundCollectionView. contentInset. left
143+ let index = Int ( round ( offset / totalCellWidth) )
144+
145+ owner. floorSegmentedControl. selectedSegmentIndex = index
146+ }
147+ . disposed ( by: disposeBag)
116148
117149 nextButton. buttonTap
118150 . bind { [ weak self] in
@@ -135,7 +167,7 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
135167 titleLabel,
136168 explainLabel,
137169 floorSegmentedControl,
138- classroomCollectionView ,
170+ backgroundCollectionView ,
139171 nextButton
140172 ] . forEach { view. addSubview ( $0) }
141173 }
@@ -153,7 +185,7 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
153185 $0. leading. trailing. equalToSuperview ( )
154186 $0. height. equalTo ( 25 )
155187 }
156- classroomCollectionView . snp. makeConstraints {
188+ backgroundCollectionView . snp. makeConstraints {
157189 $0. top. equalTo ( floorSegmentedControl. snp. bottom) . offset ( 24 )
158190 $0. leading. trailing. equalToSuperview ( ) . inset ( 24 )
159191 $0. bottom. equalToSuperview ( )
0 commit comments