I want to implement a function: swipe to select multiple griditems, just like the functionality in FileManager of IOS:
- an item with a Text(“Hello”) and a Text(“World”) are displayed in the lazyvgrid of scrollview
- swipe on the Text(“Hello”), the items passed through are selected and scrollview does not response to scroll, while scrollview will scroll automatically when user remains finger on the 1st or lastest griditem
- swipe on the file name, scrollview is responsed to scroll normally
.gesture attached in the griditem and a timer fired to scroll item can achieve the requirements except that I find the griditem will be reused while scrolling which means that .gesture’s onChange and onEnd will not called any more, is there any way to fix the problem? Thanks a lot.
ScrollViewReader { proxy in
ScrollView {
let dispWidth = CGFloat(UIScreen.main.bounds.width * 9 / 10)
LazyVGrid(columns: [GridItem(.adaptive(minimum: 200, maximum: dispWidth), alignment: .trailing)], spacing: 10) { ForEach(0..<itemsCount, id: .self) { index in
GeometryReader { geometry in
HStack {
Text("Hello (index):")
.frame(width: 100)
.border(.blue, width: isItemSelected(index: index) ? 1 : 0)
.overlay {
GeometryReader { geometry -> Color in
let frame = geometry.frame(in: .named(containerSpace))
itemsRect.content[index] = frame
return Color.clear
}
}
.gesture(dragSelect(proxy: proxy, geometry: geometry))
Text("World No# (index)")
.frame(width: 200)
}
}
}
}
}
.overlay {
GeometryReader { proxy -> Color in
scrollviewHeight = proxy.size.height
return Color.clear
}
}
.coordinateSpace(name: containerSpace)
}
New contributor
xunyang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.