Sunday, April 8, 2012

(iPhone) How to handle touches on a UITextView?


I'm trying to handle touches on a iPhone's UITextView. I successfully managed to handle taps and other touch events by creating a subclass of UIImageViews for example and implementing the touchesBegan method...however that doesn't work with the UITextView apparently :(



The UITextView has user interaction and multi touch enabled, just to be sure...no no joy. Anyone managed to handle this?


Source: Tips4all

4 comments:

  1. UITextView (subclass of UIScrollView) includes a lot of event processing. It handles copy and paste and data detectors. That said, it is probably a bug that it does not pass unhandled events on.

    There is a simple solution: you can subclass UITextView and impement your own touchesEnded (and other event handling messages) in your own versions, you should call[super touchesBegan:touches withEvent:event]; inside every touch handling method.

    #import "MyTextView.h" //MyTextView:UITextView
    @implementation MyTextView

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan");
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    NSLog(@"touchesMoved");
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"****touchesEnded");
    [self.nextResponder touchesEnded: touches withEvent:event];
    NSLog(@"****touchesEnded");
    [super touchesEnded:touches withEvent:event];
    NSLog(@"****touchesEnded");
    }

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touches... etc];
    NSLog(@"touchesCancelled");
    }

    ReplyDelete
  2. I'm using a textview as a subview of a larger view. I need the user to be able to scroll the textview, but not edit it. I want to detect a single tap on the textview's superview, including on the textview itself.

    Of course, I ran into the problem that the textview swallows up the touches that begin on it. Disabling user interaction would fix this, but then the user won't be able to scroll the textview.

    My solution was to make the textview editable and use the textview's shouldBeginEditing delegate method to detect a tap in the textview. I simply return NO, thereby preventing editing, but now I know that the textview (and thus the superview) has been tapped. Between this method and the superview's touchesEnded method I have what I need.

    I know that this won't work for people who want to get access to the actual touches, but if all you want to do is detect a tap, this approach works!

    ReplyDelete
  3. You need to assign the UITextView instance.delegate = self (assuming you want to take care of the events in the same controller)

    And make sure to implement the UITextViewDelegate protocol in the interface... ex:

    @interface myController : UIViewController <UITextViewDelegate>{
    }


    Then you can implement any of the following


    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
    - (BOOL)textViewShouldEndEditing:(UITextView *)textView;

    - (void)textViewDidBeginEditing:(UITextView *)textView;
    - (void)textViewDidEndEditing:(UITextView *)textView;

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
    - (void)textViewDidChange:(UITextView *)textView;

    - (void)textViewDidChangeSelection:(UITextView *)textView;

    ReplyDelete
  4. If you want to handle single/double/triple tap on UITextView, you can delegate UIGestureRecongnizer and add gesture recognizers on your textview.

    Heres sameple code (in viewDidLoad):

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];

    //modify this number to recognizer number of tap
    [singleTap setNumberOfTapsRequired:1];
    [self.textView addGestureRecognizer:singleTap];
    [singleTap release];


    and

    -(void)handleSingleTap{
    //handle tap in here
    NSLog(@"Single tap on view");
    }


    Hope this help :D

    ReplyDelete