5

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

杂注标记键盘

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

和:

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

谢谢你的帮助。

4

4 回答 4

7

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()
}
于 2016-10-21T16:40:20.047 回答
2

您可以通过管理按钮的事件来做到这一点,例如 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];

-(无效)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];

-(无效)btnTouchUp

[self.kpTimer invalidate];
self.kpTimer = nil;
于 2017-01-06T10:40:13.867 回答
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];
}
于 2015-07-24T06:22:32.387 回答
0

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

UICollectionViewCell 上的长按手势

于 2014-09-02T22:22:07.713 回答