DeveloBär
DeveloBär

Reputation: 671

WKLongPressGestureRecognizer in WKInterfaceTable

I am trying to add a WKLongPressGestureRecognizer to cells in a WKInterfaceTable. But it does not work. The IBActions are never fired. Is there any trick that I'm missing? Or is this simply not supported on watchOS?

interface builder

Upvotes: 3

Views: 308

Answers (1)

Marcus
Marcus

Reputation: 271

In case it's helpful... I've been battling the same and got to: Attaching the 'Long press gesture recogniser' under 'Table' in your screenshot, rather than 'Group' and linking to an Action in the WKInterfaceController:

- (IBAction)didLongPress:(id)sender
{
if ([sender isKindOfClass:[WKLongPressGestureRecognizer class]])
    {
    WKLongPressGestureRecognizer * item=(WKLongPressGestureRecognizer *)sender;
    CGPoint p=[item locationInObject];
    NSLog(@"long press point: %f , %f. state=%d\n",p.x,p.y,[item state]);
    }
}

It's not a final solution, but the action does at least get triggered. Unfortunately when triggered, it stops the didSelectRowAtIndex coming through so it's not easy to figure which row has been long-pressed. My best thought for progressing would be to use the p.y value to determine which row in the table was long-pressed on. I've been trying to figure out how to programmatically determine row heights or translate a location into an object but have so far failed, so for the moment I have given up on this functionality as I don't really want to hard-code a row-height in. But maybe it gets someone else closer to a solution, or fits a situation where row-height can be safely be hard-coded :)

Other routes I tried (and failed), but might be a starting point for others (or save some time):

  • trying to cancel the long press, so I still get the row-select come through (but I could have recorded we have a long press, so can act accordingly). Unfortunately I couldn't see how to do this. I tried [item setEnabled:false]; but it didn't cancel the current long press. It just stopped all future long presses being recognised.
  • I looked up programmatically attaching gestures in case this would give greater control than using storyboard. I didn't get anywhere, and my research indicates this is only supported in iOS, not WatchOS.
  • Attaching a gesture as per the posted question. I too was unable to see evidence of any action code being called :(

Upvotes: 1

Related Questions