• 1000 OnScroll и использование contentOffset не приводит к смещению ячеек.
Как я могу получить к ним доступ? т.е. программная установка позиции коллекций через .contentOffset и функцию onScroll UICollectionView, когда она находится внутри UIViewRepresentable? Мой код пока что ниже // ------------------------------------------ --------------------------------
импорт SwiftUI
struct col3: UIViewRepresentable {@State var data: [Int] = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,20 , 21,22,23,24,25,26,27,28,29,30]
func makeUIView(context: Context) -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.dataSource = context.coordinator
collectionView.delegate = context.coordinator
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
collectionView.backgroundColor = .clear
collectionView.contentOffset = CGPoint(x: 200, y: 0) // not having an effect
return collectionView
}
//onScroll ?
func updateUIView(_ uiView: UICollectionView, context: Context) {
uiView.reloadData()
print("advasdvasdvfdv")
}
func makeCoordinator() -> Coordinator {
return Coordinator(data: data)
}
class Coordinator: NSObject, UICollectionViewDelegate, UICollectionViewDataSource {
var data: [Int]
init(data: [Int]) {
self.data = data
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath)
let textLable = UILabel()
textLable.text = String(data[indexPath.row])
cell.addSubview(textLable)
cell.backgroundColor = .red
return cell
}
//something more
private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
print("User tapped on item \(indexPath.row)")
}
}
}