我有一个类,它被设置为在成员变量UICollectionView中触发移动函数:
class CollectionFunctionStore : ObservableObject {
//class to hold data about a UICollection and from where some functions can be triggered
var collectionRef : UICollectionView?
func jumpGrid(){
if let lclCollection = collectionRef {
let ip = IndexPath(row: 15, section: 0)
lclCollection.scrollToItem(at: ip, at: .bottom, animated: false)
lclCollection.reloadData()
print("jumpGrid() was reached: ")
}
}
}这里的单个函数"jumpGrid“将把网格向下弹到第15行,这个类正在swiftUI应用程序中使用,而有问题的集合包含在UIViewRepresentable中。函数存储和包含UICollectionView的UIViewRepresentable都在一个结构中,顶层内容视图中有一个按钮
import SwiftUI
struct ContentView: View {
@ObservedObject var swiperStore = CollectionFunctionStore()
var body: some View {
return ZStack {
VStack{
CollectionAndStoreContainer(swiperStore: swiperStore)
JumpButton(swiperStore: swiperStore)
}.onAppear(perform: swiperStore.jumpGrid)
}
}
}
struct CollectionAndStoreContainer : View {
@ObservedObject var swiperStore : CollectionFunctionStore
var body: some View{
return ZStack{
CollectionViewRepresentable(swiperStoreParam: swiperStore).frame(width: 30, height: 200)
}
}
}
struct JumpButton : View {
@ObservedObject var swiperStore : CollectionFunctionStore
var body : some View{
return Button(action: {
swiperStore.jumpGrid()
}){
ZStack {
Rectangle().frame(width: 60, height: 30).foregroundColor(.orange).cornerRadius(4)
Text("j").foregroundColor(.white)
}
}
}
}我面临的问题是:包含与顶级视图相同的函数存储的按钮可以触发scroll to item函数而不会出现任何问题,而onAppear事件可以调用该函数,它将实际打印包含在其代码中的消息,但它不会移动集合的内容……按钮调用该函数,UICollectionView正确响应,onAppear调用该函数,但UICollectionView不移动。
=Some additional info=在这种情况下我在使用UIViewRepresentable时没有选择,我不能使用lazyHgrid或HStacks这里是有问题的UIViewRepresentable的代码
struct CollectionViewRepresentable : UIViewRepresentable {
var swiperStore : CollectionFunctionStore
typealias UIViewType = UICollectionView
init(swiperStoreParam : CollectionFunctionStore) {
swiperStore = swiperStoreParam
}
func makeUIView(context: Context) -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 30, height: 30)
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.minimumLineSpacing = 0
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.dataSource = context.coordinator
collectionView.delegate = context.coordinator
collectionView.register(ExtendedCell.self, forCellWithReuseIdentifier: "ExtCell")
collectionView.isScrollEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
swiperStore.collectionRef = collectionView
return collectionView
}
func makeCoordinator() -> Coordinator {
return Coordinator()
}
func updateUIView(_ uiView: UICollectionView, context: Context) {
}
}有人有什么想法吗?我们的总体目标是让UICollectionView在它出现的一开始就滚动到一个特定的位置,所以我认为在SwiftUI中使用onAppear是最好的方法,但由于某种原因,它不能移动网格,例如,在协调器中有没有更好的选项?感谢任何帮助..
发布于 2020-11-14 22:41:54
我认为现在在onAppear中调用scroll-to还为时过早,因为当时集合视图还没有完成构造。
试一试
}.onAppear {
DispatchQueue.main.async { swiperStore.jumpGrid() }
}甚至是
}.onAppear {
// with some delay, if appeared constructed with animation or async, etc.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // 0.25-0.5 sec
swiperStore.jumpGrid()
}
}https://stackoverflow.com/questions/64834603
复制相似问题