【问题标题】:ios 8 custom keyboard hold button to delete?ios 8自定义键盘按住按钮删除?
【发布时间】:2014-10-27 07:07:52
【问题描述】:

我目前正在构建一个自定义键盘,我几乎完成了。我遇到的一个问题是删除按钮。当用户点击删除按钮时,它会做它应该做的事情并删除之前的文本条目。但是,当用户按住按钮时,什么也没有发生。如何做到这一点,当用户按住删除按钮时,键盘会像标准 ios 键盘一样连续删除?这是我当前的代码:

pragma mark 键盘

- (void)addGesturesToKeyboard{
[self.keyboard.deleteKey addTarget:self action:@selector(pressDeleteKey)forControlEvents:UIControlEventTouchUpInside];

和:

-(void)pressDeleteKey{
[self.textDocumentProxy deleteBackward];
}

感谢您的帮助。

【问题讨论】:

    标签: ios keyboard ios8 ios-app-extension custom-keyboard


    【解决方案1】:

    一触屏就设置一个计数器,比如2-5秒。 这种情况称为长按手势,这里是类似问题的链接。

    Long press gesture on UICollectionViewCell

    【讨论】:

    • 最佳做法是在答案中包含所有详细信息。如果你只是想链接那更多的是评论。
    • 如果那里有答案,我为什么还要再回答?由于链接非常详细,因此我无法击败那个人。
    • 如果您认为问题相同,则为重复。如果您要通过展示其他人的解决方案来“回答”,您应该发表评论。如果您有自己提供的解决方案,则应在答案中写下所有详细信息。
    • @LuoSen 如果链接的答案被删除,您的答案也将无效
    • 这并不能完全回答问题。它没有解释如何进行重复删除。
    【解决方案2】:
    - (void)addGesturesToKeyboard{
    
     UILongPressGestureRecognizer *ges = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        ges.minimumPressDuration = 0.1;
        ges.numberOfTouchesRequired = 1;
        ges.delegate = self;
        [self.mykeyboard.deleteKey addGestureRecognizer:ges];
    }
    
    - (void)longPress:(UILongPressGestureRecognizer*)gesture {
    
    
            [self.textDocumentProxy deleteBackward];
    }
    

    【讨论】:

      【解决方案3】:

      Swift 3 使用 "allowableMovement" 属性

      override func viewDidLoad() {
          super.viewDidLoad()
      
          let longPress = UILongPressGestureRecognizer(target: self, action: #selector(KeyboardViewController.handleLongPress(_:)))
          longPress.minimumPressDuration = 0.5
          longPress.numberOfTouchesRequired = 1
          longPress.allowableMovement = 0.1
          buttonDelete.addGestureRecognizer(longPress)
      }
      
      func handleLongPress(_ gestureRecognizer: UIGestureRecognizer) {
          textDocumentProxy.deleteBackward()
      }
      

      【讨论】:

        【解决方案4】:

        您可以通过管理按钮的事件来做到这一点,例如 touchdown、touchupinside 和 touchoutside。

        当按下按钮时,延迟 0.2 秒启动计时器并从 textDocumentProxy 中删除最后一个字符,直到按钮的 touchup 方法触发,之后您只需使计时器无效。

        [self.btnDelete addTarget:self action:@selector(btnTocuhDown:) forControlEvents:UIControlEventTouchDown];
        [self.btnDelete addTarget:self action:@selector(btnTouchUp:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnDelete addTarget:self action:@selector(btnTouchUp:) forControlEvents:UIControlEventTouchUpOutside];
        

        -(void) btnTocuhDown

            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2  target:self selector:@selector(kpTimerMethod:) userInfo:nil repeats:YES];
        
        self.kpTimer = timer;
        __weak typeof(self)weakSelf = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
            if (timer == self.kpTimer) {
                [weakSelf.kpTimer fire];
            }
        });
        

        -(void)kpTimerMethod:(NSTimer *)timer

        if (self.btnDelete.highlighted)
        {
            [self deleteLastCharacter];
        }
        else
        {
            [timer invalidate];
            self.kpTimer = nil;
        }
        

        -(void)deleteLastCharacter

        NSString *strInput = self.textDocumentProxy.documentContextBeforeInput;
        
        if (strInput.length > 1)
            NSString *coupleOfLastCharacters = [strInput substringWithRange:NSMakeRange(strInput.length-2, 2)];
            if( [@"yo" caseInsensitiveCompare:coupleOfLastCharacters] == NSOrderedSame ) {
                [self.textDocumentProxy deleteLastCharacter];
            }
        }
        [self.textDocumentProxy deleteLastCharacter];
        

        -(void) btnTouchUp

        [self.kpTimer invalidate];
        self.kpTimer = nil;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-06
          • 2010-12-03
          • 2020-02-16
          • 2023-04-01
          • 1970-01-01
          • 2014-11-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多