Longpress and list scrolling

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

Answered by Claude31 in 399649022

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)}
      }
    }
  }
}

I've put together a small example that show this issue:


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{}
      }
    }
  }
}


If I try to drag the list pressing on any text, the list wont move. If I remove the longpress handler, it moves no matter where I press down.

Accepted Answer

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)}
      }
    }
  }
}

Thank you very much for pointing this out. It solved my problem. I didn't considder having an empty onTapGesture handler would have any impact on the scrolling, but it does. Maybe the common use is to have the tap handler and then add longtap as an additional gesture (makes sense).
Thanks again :-)

Longpress and list scrolling
 
 
Q