需要双击而不是单击的键盘
问题描述:
我不想在文本框上单击显示键盘,而是想要双击显示它。 为此,我创建了两个动作,一个是单一的水龙头及其他的双击,也使用文本框的委托方法:需要双击而不是单击的键盘
[textField1 addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchDownRepeat]; // for double tap
[textField1 addTarget:self action:@selector(clicked1) forControlEvents:UIControlEventTouchDown]; // for single tap
METHOD DEFINATION:
-(void)clicked // action for double tap
{
count=2;
NSLog(@"--------double------%d",count);
[textField1 becomeFirstResponder];
}
-(void)clicked1 //action for single tap
{
count=1;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField //textfield delegate
{
if(count==1)
return NO;
else
return YES;
}
此代码工作正常,但并非总是如此。有时未检测到“UIControlEventTouchDownDownRepeat”事件,并且“单击”功能不起作用。 我无法理解这个原因,为什么这个事件不是每次都有效。
答
嗨,你可以试试这个。
//Tap GestureRecognizer to recognize tap
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(youMethodName)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
//使用yourMethodName显示键盘
答
不要添加任何行动文本框 做类似下面
@interface ViewController()<UIScrollViewDelegate,UITextFieldDelegate>
{
int count;
}
- (void)viewDidLoad
{
count = 0;//initially put zero
self.textField.delegate = self; //textfield added in xib or story board
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
count++;
if(count == 2)
{
count = 0;
return YES;
}
if(count >= 2)
count = 0;
return NO;
}
//for testing i am doing like this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
重新编辑你的问题不要求用不同的岗位同样的问题 – codercat