ios – 如何检测UITextField上的点击?

我有一个UITextField禁用用户交互.所以如果你点击这个文本字段,没有任何反应.通常检查文本字段是否被点击Id尝试委托方法,但是我不能因为禁用用户交互.有没有办法检查文本字段是否被点击/触摸?我将另一个元素改为hidden = no;当它被轻拍时,我想知道它是否可以启用用户交互.

解决方法

也许,您可以在超级视图中添加UItapGestureRecognizer,检测触摸是否在框架内,并执行某些操作

Objective-C的

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizeTapGesture:)];
[self.textField.superview addGestureRecognizer:tapGesture];

接着:

- (void)didRecognizeTapGesture:(UITapGestureRecognizer*)gesture {
    CGPoint point = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateEnded) {
        if (CGRectContainsPoint(self.textField.frame,point)) {
            [self doSomething];
        }
    }
}

迅速

let tapGesture = UITapGestureRecognizer(target: self,action: #selector(didRecognizeTapGesture(_:)))
textField.addGestureRecognizer(tapGesture)

然后

private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
    let point = gesture.location(in: gesture.view)
    guard gesture.state == .ended,textField.frame.contains(point) else { return }
    //doSomething()
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...