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

前端之家收集整理的这篇文章主要介绍了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()
}
原文链接:https://www.f2er.com/iOS/336663.html

猜你在找的iOS相关文章