ios 8 custom keyboard hold button to delete?

前端 未结 4 528
无人共我
无人共我 2021-01-13 12:03

I am currently building a custom keyboard and I am almost done. One problem that I have is with the delete button. When the user taps the delete button, it does what it shou

4条回答
  •  情话喂你
    2021-01-13 12:09

    you can do this by managing button’s events like touchdown, touchupinside and touchoutside.

    When button press at that time start timer with delay of 0.2 seconds and delete last characters from textDocumentProxy until button’s touchup method will fire and after that you just need to invalidate timer.

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

提交回复
热议问题