1

i need to get the coordinate position from touching a view. The view i touch is UITextview. THe hierarchy of the view is UIViewController -> UITextview. I think i can use locationInView method to get that coordinate. I know that UITextview have some delegate methods, so i think i have to use textViewShouldBeginEditing delegate method to call the locatioInView.

This is my code snippet

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {

  NSSet *touches;
  UITouch *touch = [touches anyObject];

  CGPoint currentTouchPosition = [touch locationInView:textView];
  NSLog(@"currentTouchPosition: x=%f, y=%f", currentTouchPosition.x, currentTouchPosition.y);

  return YES;
}

my problem is, the currentTouchPosition always have 0 value in x and y. I dont know what's wrong with that code. Can somebody tell me???

thank you

UPDATE Finally i've solved this problem. I have use UITapGestureRecognizer in viewDidLoad and then get the tap location using the same method i was use in textViewShouldBeginEditing. This is the code i've made :

- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
tap.delegate=self;
[self.textView addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;

[tap release];
}

and this is the handleTapFrom :

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {

locationTap = [recognizer locationInView:self.textView];

}
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
R. Dewi
  • 4,141
  • 9
  • 41
  • 66

1 Answers1

0

Your simply declaring one NSSet object and retrieving data from it which always gives eampty. Take a subClass of UITextView and write down touch begin method in it i.e -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event.The parameter touches will then give you the correct touch point on the view.Use your subclass object as your TextView.

Hariprasad
  • 1,094
  • 2
  • 12
  • 30
  • thank you for answer, i was try it, i made a subclass of UITextview named customTextView, but in this app, this method never fired – R. Dewi Mar 17 '11 at 01:52
  • did you define body for the "touchesBegan" method in your customTextView – Hariprasad Mar 17 '11 at 06:25
  • yes, i was define the code, it is exactly the same like i do in textViewShouldBeginEditing above, but it no nslog print in the debugger – R. Dewi Mar 17 '11 at 06:57
  • you should use the touch variable from parameter in your method.any way u solved this problem by using tap gesture ok. – Hariprasad Mar 17 '11 at 09:37
  • yes...finally i've solved this problem, but it gives me another bug, the selector method that fired in the tap is sometime called after textviewDidChangeSelection delegate method and sometime called before it. I need it called before that delegate method, do you know how to fixed it?? – R. Dewi Mar 17 '11 at 09:50
  • tap gesture selector is fired when ever tap is occur on the view.if you implement the touch methods(touchesBegin) properly,the code which you want to fire will be executed. – Hariprasad Mar 17 '11 at 13:01