【问题标题】:Need keyboard on double tap and not on single tap [duplicate]需要双击键盘而不是单击键盘[重复]
【发布时间】:2014-02-25 09:09:49
【问题描述】:

我不想在单击文本字段时显示键盘,而是想在双击时显示它。 为此,我创建了两个动作,一个用于单击,另一个用于双击,还使用了文本字段委托方法:

            [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;
}

此代码运行良好,但并非总是如此。有时“UIControlEventTouchDownRepeat”事件未被检测到,“点击”功能不起作用。 我无法理解为什么该事件每次都不起作用。

【问题讨论】:

  • 重新编辑你的问题,不要在不同的帖子里问同样的问题

标签: ios iphone ios5 ios6 ios7


【解决方案1】:

你好,你可以试试这个。

//Tap GestureRecognizer to recognize tap
     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(youMethodName)];
        doubleTap.numberOfTapsRequired = 2; 
        [self.view addGestureRecognizer:doubleTap];

//使用 yourMethodName 显示键盘

【讨论】:

    【解决方案2】:

    不要为文本字段添加任何操作 如下所示

       @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;
      }
    

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 2015-02-21
      相关资源
      最近更新 更多