简体   繁体   English

在已激活的UITextField上检测点击

[英]Detect a Tap on an Already Activated UITextField

I have a UITextField and it is activated (the caret is blinking). 我有一个UITextField并被激活(插入符号闪烁)。 I want to detect when the user taps the UITextField (where default behavior is to show/hide the "Select/SelectAll/Paste" context menu). 我想检测用户何时点击UITextField(默认行为是显示/隐藏“ Select / SelectAll / Paste”上下文菜单)。 I've tried several things, one of which was a UITapGestureRecognizer: 我已经尝试了几件事,其中之一是UITapGestureRecognizer:

self.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self addGestureRecognizer:tap];
tap.delegate = self;

Simply tapping the UITextField produces no call to my handler. 只需轻按UITextField,就不会调用我的处理程序。

- (void)tap:(UITapGestureRecognizer *)tap
{
    if (tap.state == UIGestureRecognizerStateBegan)
    {
        // do something
    }

    if (tap.state == UIGestureRecognizerStateEnded)
    {
        // do something
    }
}

"Swiping" the UITextField produces the call (which doesn't solve the problem), and even so, tap.state is always UIGestureRecognizerStateEnded (I would like to be able to differentiate between the TouchDown and the TouchUp). “刷” UITextField会产生调用(无法解决问题),即使如此,tap.state始终是UIGestureRecognizerStateEnded(我希望能够区分TouchDown和TouchUp)。

Any suggestions? 有什么建议么?

How about adding a transparent view (or 99% transparent) overlaid on the text field, and put the gesture recognizer in that view? 如何在文本字段上添加透明视图(或99%透明),然后将手势识别器放在该视图中呢?

Insert the view when the user activates it, and remove it when the text view is dismissed. 用户激活时插入该视图,而关闭文本视图则将其删除。

As I recall you may need to use a transparency of alpha = 0.999;, as I seem to recall completely transparent views won't have their gesture recognizers activated (but I could be wrong, or my info out of date.) 我记得您可能需要使用alpha = 0.999;的透明度,因为我似乎记得完全透明的视图不会激活其手势识别器(但是我可能错了,或者我的信息已经过时了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM