2014-09-02 95 views
4

我目前正在構建一個自定義的鍵盤,我差不多完成了。我有一個問題是刪除按鈕。當用戶點擊刪除按鈕時,它將執行該操作並刪除以前的文本條目。但是,當用戶按下按鈕時,什麼都不會發生。我該如何做到這一點,以便當用戶按住刪除按鈕時,鍵盤會不斷刪除,就像標準的ios鍵盤一樣?這是我當前的代碼:ios 8自定義鍵盤保持按鈕刪除?

編譯標記鍵盤

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

和:

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

感謝您的幫助。

回答

0

觸摸屏幕後立即設置計數器,例如2-5秒。 這種情況稱爲長按手勢,這裏是鏈接到同樣的問題。

Long press gesture on UICollectionViewCell

+0

這是包含答案中所有細節的最佳做法。如果你只是想鏈接更多的評論。 – 2014-09-02 22:28:07

+0

如果有答案,爲什麼我應該再回答?由於鏈接非常詳細,我無法擊敗這個人。 – 2014-09-02 22:29:51

+2

如果你認爲問題是相同的,它是重複的。如果您要通過展示某人else解決方案來「回答」您應該評論。如果你有自己提供的解決方案,你應該在答案中寫下所有的細節。 – 2014-09-02 22:35:46

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

斯威夫特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() 
} 
2

您可以通過管理按鈕的事件,如着陸,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]; 
    } 
}); 

- (無效)kpTimerMethod:(的NSTimer *)定時器

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

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