将 TapGestureRecognizer 添加到 UITextView

发布于 2024-11-14 04:00:32 字数 450 浏览 2 评论 0原文

我想将 *UITapGestureRecognize*r 添加到我的 UITextView 中,因为我想关闭 TextView 所在的“弹出窗口”。所以我想要该方法“当 T*extView* 被点击时,Popup 类的 hide" 被调用。我尝试了如下方法,但由于某种原因它不起作用:

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(show)];
[gr setNumberOfTapsRequired:1];
[viewText addGestureRecognizer:gr];

我也不想为其创建子类,因为然后我需要调用“父”方法“隐藏”。

也许您现在可以很好地解决该问题。
先感谢您。

I want to add an *UITapGestureRecognize*r to my UITextView, because I want to close a "Popup" where the TextView is in. So I want, that the method "hide" of the Popup class is called, when the T*extView* is tapped. I tried it like the following, but it isn't working for some reason:

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(show)];
[gr setNumberOfTapsRequired:1];
[viewText addGestureRecognizer:gr];

I also don't want to create a Subclass for it, because I then would need to call the "parent"-method "hide".

Maybe you now a good solution for that problem.
Thank you in advance.

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

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

发布评论

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

评论(3

把昨日还给我 2024-11-21 04:00:32

您不应该使用 UITapGestureRecognizer,而应使用 UITextFieldDelegate。

您可以在这里阅读:

您基本上需要添加
UITextViewDelegate 像这样到你的 .h 文件 -

@interface MyViewController : UIViewController<UITextViewDelegate>

然后将你的控制器分配为委托:

viewText.delegate =self;

现在使用委托方法之一,也许:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

   // Do what you need to do...

}

编辑

好吧,我可以考虑另外 2 种方法:

  1. 你可以将你的 textView 包装在里面UIView 并将 UITapGestureRecognizer 添加到视图中。
  2. 您可以使用:

     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         UITouch *touch = [触摸任何对象];
         CGPoint 位置 = [触摸 locationInView:textView];
    
         //检查点击是否在文本视图范围内
         if (CGRectContainsPoint(textView.bounds, 位置)){
             //做某事
         }
     }
    

祝你好运

You shouldnt use UITapGestureRecognizer but use the UITextFieldDelegate.

You can read about it here:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/doc/uid/TP40006897

You basicly need to add the
UITextViewDelegate to your .h file like that -

@interface MyViewController : UIViewController<UITextViewDelegate>

Then assign your controller as the delegate:

viewText.delegate =self;

Now use one of the delegation methods, maybe:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

   // Do what you need to do...

}

Edit

Well I can think on 2 additional approaches:

  1. You can wrap your textView inside a UIView and add the UITapGestureRecognizer to the view.
  2. You can use :

     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         UITouch *touch = [touches anyObject];
         CGPoint location = [touch locationInView:textView];
    
         //Checks if the tap was inside the textview bounds
         if (CGRectContainsPoint(textView.bounds, location)){
             //do something
         }
     }
    

Good luck

冷︶言冷语的世界 2024-11-21 04:00:32

您是否尝试过 NSLog on show 方法?或者你甚至声明并编写了方法“show”?它应该可以工作,这就是我在文本视图上所做的。

PS 在添加 textview 后不要忘记释放你的手势实例 (gr) :D

Did you try to NSLog on show method? or did you even declare and write method "show" ? It should work and that's how I did on my text view.

P.S don't forget to release your gesture instance (gr) after you add on textview :D

倒带 2024-11-21 04:00:32

我在实现此功能时也遇到了重大问题,但我遇到了一个愚蠢的问题,即可视化编辑器中的用户交互被关闭。希望这对某人有帮助:)

I had major problems getting this working also but I had one stupid problem, user interaction was turned off in the visual editor. Hope this helps someone :)

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