我有一个禁用用户交互的UITextField。因此,如果您点击此文本字段,则不会发生任何事情。通常检查文本字段是否被点击Id尝试委托方法,但我不能,因为禁用了用户交互。有什么方法可以检查文本字段是否被轻击/触摸?我将另一个元素更改为hidden = no;当它被点击时,我想知道它是否可能实现用户交互。
答案 0 :(得分:39)
最佳选择是启用用户交互并使用委托方法禁用编辑操作。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
您可以在该功能中调用您的方法来检测点按。
答案 1 :(得分:24)
也许,您可以在超级视图中添加UITapGestureRecognizer
,检测触摸是否在框架内,然后执行某些操作。
UITapGestureRecognizer
并将其添加到UITextField
的超级视图中。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];
}
}
}
func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))
textField.superView?.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()
}