ios 8自定义键盘按住按钮删除?

时间:2014-09-02 22:11:44

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

我目前正在构建一个自定义键盘,我差不多完成了。我遇到的一个问题是删除按钮。当用户点击删除按钮时,它会执行应该执行的操作并删除以前的文本条目。但是,当用户按下按钮时,没有任何反应。如何使用当按下删除按钮时,键盘会像标准ios键盘一样连续删除?这是我目前的代码:

pragma mark键盘

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

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

感谢您的帮助。

4 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

您可以通过管理按钮,触摸屏和触摸屏等按钮来执行此操作。

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

    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)btnTocuhDown

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

- (void)kpTimerMethod:(NSTimer *)timer

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

- (无效)deleteLastCharacter

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

- (void)btnTouchUp

appearance<0,1,0,1>::value // should return true
appearance<2,0,1,2,2>::value // should return false
appearance<5,5,5>::value // should return false 

答案 2 :(得分: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];
}

答案 3 :(得分:0)

触摸屏幕后立即设置计数器,例如2-5秒。 这种情况称为长按手势,这里是simliar问题的链接。

Long press gesture on UICollectionViewCell