Hi,
Using SwiftUI I'm trying to implement a long press gesture on items in a list, to allow user interaction with the individual items. The problem is that when I set "onLongPressGesture" anywhere in the list (on items, on the list itself), the list cannot be scrolled anymore. I can easily get a simple tap to work but a long press blocks scrolling.
I've been Googling this for a couple of days now without any success. Hope someone can point me in the right direction.
Yours,
Jens
I tested on simulator in playground on Mojave
struct ContentView: View
{
let data = [
"Test 1","Test 2","Test 3","Test 4","Test 5",
"Test 6","Test 7","Test 8","Test 9","Test 10",
"Test 11","Test 12","Test 13","Test 14","Test 15",
"Test 16","Test 17","Test 18","Test 19","Test 20"
]
var body: some View
{
List
{
ForEach(data, id:\.self)
{
item in
Text(item).onLongPressGesture{ print(item)}
}
}
}
}
I works OK:
- I get the print with longPress on item name
Test 6
Test 10
- I can scroll the list later, by dragging on cell, but not taping on the label itself (but on the empty part of cell).
adding onTapGesture before onLongPressGesture makes it work all wherever you drag
struct ContentView: View
{
let data = [
"Test 1","Test 2","Test 3","Test 4","Test 5",
"Test 6","Test 7","Test 8","Test 9","Test 10",
"Test 11","Test 12","Test 13","Test 14","Test 15",
"Test 16","Test 17","Test 18","Test 19","Test 20"
]
var body: some View
{
List
{
ForEach(data, id:\.self)
{
item in
Text(item)
.onTapGesture { }
.onLongPressGesture{ print("longPressed", item)}
}
}
}
}