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

Accepted Reply

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)}
      }
    }
  }
}
  • Same issue with Texfield and LongPress. Put empty onTapGesture before LonPress solve the issue Thanks a lot!

  • It actually doesn't fix the problem. If you have this inside a ItemCell structure and you have a NavigationLink, the empty onTapGesture will fire when the user taps the cell, and the navigation will not work anymore. I'm trying to figure out the solution for it.

  • I was facing same issue. This solution is worked. Thanks a lot!

Replies

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.

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)}
      }
    }
  }
}
  • Same issue with Texfield and LongPress. Put empty onTapGesture before LonPress solve the issue Thanks a lot!

  • It actually doesn't fix the problem. If you have this inside a ItemCell structure and you have a NavigationLink, the empty onTapGesture will fire when the user taps the cell, and the navigation will not work anymore. I'm trying to figure out the solution for it.

  • I was facing same issue. This solution is worked. Thanks a lot!

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