ios 8 custom keyboard hold button to delete?

南笙酒味 提交于 2019-12-01 05:57:34
Luo Sen

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

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()
}

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;
jigar parmar
- (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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!