Cocoa -touch: how to transfer touch to another object

This is a beginner's question that I fear:

I have a UIText that spans the entire screen. I have another transparent view on top of this UITextView to be able to recognize gesture movements (horizontally and vertically), for example:

- (void)viewDidLoad { [super viewDidLoad]; // UITextView CGRect aFrame = CGRectMake(0, 0, 320, 480); aTextView = [[UITextView alloc] initWithFrame:aFrame]; aTextView.text = @"Some sample text."; [self.view addSubview:aTextView]; // canTouchMe CGRect canTouchMeFrame = CGRectMake(0, 0, 320, 480); canTouchMe = [[UIView alloc] initWithFrame:canTouchMeFrame]; [self.view addSubview:canTouchMe]; } 

Let's look at how the user touches (does not check) the canTouchMe view. In this case, I would like the canTouchMe view to disappear and pass a touch to the UITextView hiding beneath it to enter edit mode and enable the β€œnatural” scroll options that the UITextView has (that is, only horizontally).

The my touchhes method began to look like this:

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; UITouch *touch =[touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } 

How to tell this method that if it only recognizes ONE touch , it should hide canTouchMeFrame and PASS ON touch in a UITextView?

Sorry if this is basic, but I have no idea how to implement this. Thanks for any suggestions.


EDIT:

I introduced the touchEnded method, but I still had no luck. Touch will not be redirected to UITextView. I need to double click to edit it:

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [super touchesMoved:touches withEvent:event]; UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive if (deltaY == 0 && deltaX == 0) { label.text = @"Touch"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; [aTextView touchesBegan:touches withEvent:event]; [self.view bringSubviewToFront:aTextView]; [self.view bringSubviewToFront:doneEdit]; } 

}

+4
source share
1 answer

NSSet has the -count method. If touches has only one object, you respond with one touch.

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; if ([touches count] == 1) { [self hideMyRectangle]; [someOtherObject touchesBegan:touches withEvent:event]; //etc, etc. return; } // if you get here, there more than one touch. UITouch *touch =[touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } 
+2
source

All Articles