5

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 should do and deletes the previous text entry. However when the user holds the button down, nothing happens. How do I make it so that when the user holds down the delete button, the keyboard continuously deletes like in the standard ios keyboard? This is my current code:

pragma mark Keyboards

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

and:

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

Thanks for your help.

Community
  • 1
  • 1

4 Answers4

7

Swift 3 Use "allowableMovement" property

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()
}
maslovsa
  • 1,589
  • 1
  • 14
  • 15
2

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;
Kalpesh Panchasara
  • 1,730
  • 2
  • 15
  • 27
1
- (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];
}
Fire Fist
  • 7,032
  • 12
  • 63
  • 109
0

Set a counter as soon as the screen is touched, such as 2-5 seconds. The situation known as Long press gesture, and here is the link to the simliar questions.

Long press gesture on UICollectionViewCell

Community
  • 1
  • 1
Luo Sen
  • 97
  • 6
  • It is a best practice to include all the details in the answer. If you just want to link that's more of a comment. – Halvor Holsten Strand Sep 02 '14 at 22:28
  • If there is a answer there, why should I answer again? And the I could not beat the guy since the link is very detailed. – Luo Sen Sep 02 '14 at 22:29
  • 2
    If you think the question is the same it is a duplicate. If you're going to "answer" by showing someone elses solution you should comment. If you have a solution that you're providing yourself you should write all the details in the answer. – Halvor Holsten Strand Sep 02 '14 at 22:35
  • 2
    @LuoSen if the linked answer gets deleted, your answer will be void too – Jasper Dec 12 '14 at 11:01
  • This does not fully answer the question. It does not explain how to do repeated deletion. – Foobar Sep 05 '16 at 01:41