开发者

How to pass on a touch to an UITextView

开发者 https://www.devze.com 2023-02-28 20:56 出处:网络
I have an UITextView which is hidden beneath another view called canTouchMe. Now, once canTouchMe is called, I bring UITextView to the front and assign it as the first responder. However, I would lik

I have an UITextView which is hidden beneath another view called canTouchMe.

Now, once canTouchMe is called, I bring UITextView to the front and assign it as the first responder. However, I would like the cursor to be placed wherever the user has touched the screen. Right now, the curser jumps to the very end of the UITextView even if the user touched the very beginning of it.

- Is there a way to pass on the touch to the UITextView?

- Or is there a way to tell the UITextView to put the cursor near the currentPosition开发者_如何学编程?

Here is my code:

 - (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) {

        NSLog(@"Touch");


        [self.view sendSubviewToBack:canTouchMe];


        [textView becomeFirstResponder];


    }


}

I tried to pass on the touch with [textView touchesBegan:touches withEvent:event]; but that didn't work.


I haven't tried this but after reading the docs for UITextView I would play with this code:

[myUITextView scrollRangeToVisible:[myUITextView selectedRange]];

this should scroll you to the place where you touched and perhaps insert a cursor there. Let me know if that worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消