cocoa-touch:如何找出哪个物体被触摸
抱歉,这是一个非常基本的初学者问题:想象一下有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试这样的事情:
You can try something like this:
注意:在这种情况下,myView 应该启用 userInteraction(myView.userInteractionEnabled = YES),否则触摸事件将传递给启用了 userInteraction 的 superView。
UITouch *touch = [触摸任何对象];
if (touch.view == myView){
}
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){
}