3

How do I enable the swipe gesture recognition for a UITextView?

This is my code and the event attach to it is not firing. It works for taps just not for swipes.

// Add swipe support for easy textview content clean up
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(eraseTextWithSwipe:)];
[targetContent addGestureRecognizer:swipe];
[swipe release];

How do I do that?

amok
  • 1,736
  • 4
  • 20
  • 41
  • I'm not sure if any of the suggestions in this question will help; there were no accepted answers in it. http://stackoverflow.com/questions/2042930/successful-swipe-in-uitextview – Greg Sep 13 '10 at 16:36
  • Thank you for your comment but that doesn't apply, I have seen it. That uses a technique that is not recommended by Apple in iPhone 4.x. – amok Sep 14 '10 at 14:16

2 Answers2

2

I found a solution that works well for me.

You need to set the delegate (referring to the code above)

swipe.delegate = self;

then you need to add a delegate to track multiple gestures, which will be able to track the swipe and the scrolling

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGesture
{
    yourTextbox.scrollEnabled = NO;
    return YES;
}

re-enable the scroll in the callback function (in the example above eraseTextWithSwipe)

amok
  • 1,736
  • 4
  • 20
  • 41
2

Thank you. Your solution works. I just had to set the delegate and return YES in the mentioned delegate method. If not for usability reasons, you don't have to disable the scrolling on the UITextView.

blomeyer
  • 59
  • 2