cocoa-touch:如何找出哪个物体被触摸

发布于 2024-11-02 11:33:38 字数 1261 浏览 6 评论 0原文

抱歉,这是一个非常基本的初学者问题:想象一下有一个 UITextField 和一个 UITextView。然后,想象一个透明的 UIView 放置在这些 TextField/View 上方,这样如果您触摸它们,它们就不会响应。

如果您触摸(而不是滑动或捏合等)UIView,我希望用户触摸的任何内容(即文本字段或视图)成为第一响应者。是否有类似以下命令:

[objectThatWasTouched comeFirstResponder];

我在下面的方法中需要这个。现在,它被设置为 UITextView 成为第一响应者,但我希望触摸的任何对象都成为第一响应者:

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

        [self.view bringSubviewToFront:aSwipeTextView];
        [self.view bringSubviewToFront:noteTitle];
        [self.view bringSubviewToFront:doneEdit];


        [aSwipeTextView becomeFirstResponder];


    }


}

最后,我需要某种方法来摆脱第一响应者,即取决于什么是第一响应者。当 UITextView 被触摸时,这只会摆脱第一响应者:

    - (IBAction)hideKeyboard
{
    [aSwipeTextView resignFirstResponder];
    [self.view bringSubviewToFront:canTouchMe];

}

Sorry, but a very basic beginner's question: Imagine having an UITextField and an UITextView. Then, imagine a transparent UIView placed above these TextField/View, so they won't respond if you touch them.

If you touch (as opposed to swipe or pinch etc) the UIView, I would like that whatever the user touches (i.e. TextField or View) becomes first responder. Is there a command like:

[objectThatWasTouched becomeFirstResponder]; ?

I would need this in the following method. Right now, it is set so that UITextView becomes first responder, but I would like whatever object is touched to become the first responder:

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

        [self.view bringSubviewToFront:aSwipeTextView];
        [self.view bringSubviewToFront:noteTitle];
        [self.view bringSubviewToFront:doneEdit];


        [aSwipeTextView becomeFirstResponder];


    }


}

And finally, I would need to some kind of method to get rid of the first responder, i.e. depending on whatever is first responder. This will get only get rid of the firstresponder, when UITextView had been touched:

    - (IBAction)hideKeyboard
{
    [aSwipeTextView resignFirstResponder];
    [self.view bringSubviewToFront:canTouchMe];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷雾森÷林ヴ 2024-11-09 11:33:38
UITouch *touch = [touches anyObject];

if (touch.view == mybutton){

        NSLog(@"My Button pressed");
}
UITouch *touch = [touches anyObject];

if (touch.view == mybutton){

        NSLog(@"My Button pressed");
}
辞旧 2024-11-09 11:33:38

你可以尝试这样的事情:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];

    UIView *hitView = [self hitTest:currentPosition withEvent:event];

    if([hitView isKindOfClass:[UIResponder class]] && [hitView canBecomeFirstResponder]) {
       [hitView becomeFirstResponder];
    }

    // ... more code ...
}

You can try something like this:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];

    UIView *hitView = [self hitTest:currentPosition withEvent:event];

    if([hitView isKindOfClass:[UIResponder class]] && [hitView canBecomeFirstResponder]) {
       [hitView becomeFirstResponder];
    }

    // ... more code ...
}
夜雨飘雪 2024-11-09 11:33:38

注意:在这种情况下,myView 应该启用 userInteraction(myView.userInteractionEnabled = YES),否则触摸事件将传递给启用了 userInteraction 的 superView。

UITouch *touch = [触摸任何对象];

if (touch.view == myView){

    NSLog(@"My myView touched");

}

Note: In this case myView should be userInteraction enabled(myView.userInteractionEnabled = YES) otherwise the touch event will be passed to its superView whose userInteraction is enabled.

UITouch *touch = [touches anyObject];

if (touch.view == myView){

    NSLog(@"My myView touched");

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文