是否可以在 iPhone 中重新创建用户手势?

发布于 2024-12-14 20:06:45 字数 544 浏览 3 评论 0原文

我正在开发一个应用程序,遇到了一个问题,归结为这种情况: 考虑一个具有两个按钮(按钮1,按钮2)和一个文本视图的超级视图,全部作为其子视图。当我单击一个按钮时,我会显示文本视图。当我点击文本视图之外的任何位置但在超级视图中时,我需要关闭文本视图。

我已将 UITapGestureRecognizer 添加到超级视图,它调用方法 tap: 。在 tap: 中,我得到了点击点,如果它位于文本视图之外,我会关闭文本视图并删除 GestureRecognizer。现在,当我点击按钮 2 时,问题就出现了。我需要关闭文本视图并执行按钮 2 的操作。但它输入了 tap: 并且我不想从那里调用按钮 2 的方法。

我想知道的是,在移除手势识别器后是否可以在相同坐标处模拟相同的点击?如果不是,我该如何解决这个问题?

编辑 我已经尝试过:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

但这并不能解决我的问题

I am developing an app and I have encountered a problem that comes down to this scenario:
Consider a superview with two buttons(button1,button2) and a text view, all as its subviews. When I click on one button, I display the text view. When I tap on anywhere outside the textview, but in the super view, I need to dismiss the text view.

I have added a UITapGestureRecognizer to the super view and it calls a method tap:. In tap:, I get the point of the tap and if it is outside the text view, I dismiss the text view and remove the GestureRecognizer. Now the problem occurs when I tap on button 2. I need to dismiss the text view as well as execute the action for button 2. But it enters tap: and I do not wish to call the button 2's method from there.

What I want to know is if it would be possible to emulate the same tap at the same co ordinates after the gesture recognizer is removed? If not what is the way I proceed to solve this issue?

EDIT
I have tried:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

But it does nt solve my problem

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

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

发布评论

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

评论(2

你在我安 2024-12-21 20:06:46

有一个简单的方法可以做到这一点。这对我来说工作得很好。请在“UIcontroleventTouchdown”事件中调用您的方法。

There is an easy way for this. This is work fine for me. Please call your method in "UIcontroleventTouchdown" event.

黯然#的苍凉 2024-12-21 20:06:45

我不知道是否可以在 iPhone 中重新创建用户手势,但您可以使用一种简单的方法来实现您所需要的。您可以使用一个方法,

// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

您可以返回 UIGestureRecognizer 是否应该接收触摸,或者换句话说是否应该执行与手势识别器绑定的方法。如果您返回NO,它将把触摸传递给子视图。

不要忘记设置手势识别器的delegate。并在.h文件中包含UIGestureRecognizerDelegate协议

您可以检查触摸是否在按钮框架内,然后您可以返回NO,否则返回是的
以下是文档 UIGestureRecognizer <一个href="http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIGestureRecognizerDelegate" rel="nofollow">UIGestureRecognizerDelegate< /a>

I don't know about is it possible to recreate User Gestures in iPhone or not but there is an easy way around you can use to achieve what you need. There is a method you can use is

// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

You can return whether the UIGestureRecognizer should receive touch or in other words should execute the method bound with gesture recognizer. If you return NO it will pass the touch to the subviews.

Do not forget to set delegate of the gesture recognizer. and including UIGestureRecognizerDelegate protocol in .h file

You can check if the touch is within the button frame then you can return NO else return YES
Here are the docs UIGestureRecognizer UIGestureRecognizerDelegate

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