2012-10-20 63 views
0

我有一個uitextview是可編輯的。我試圖添加一個退格刪除按鈕,模仿標準鍵盤退格鍵/刪除按鈕操作。觸摸重複功能。uitextview刪除文本按鈕touchhold重複

+0

好的。你有什麼嘗試?你的問題是什麼?你有什麼需要幫助的?您是否暗示您正在使用您自己的自定義inputView?獲得良好答案的最佳方式是提出一個好的,完整的,明確的問題。 – rmaddy

+0

u touchhold標準鍵盤刪除鍵,它重複。我需要一個按鈕,模仿這個動作。我看了NSTimers.A的例子將不勝感激。或者只是有一些簡單的我錯過了。 – user1725650

+0

使用計時器是我會使用的方法。當用戶按下按鈕時啓動重複計時器。當用戶釋放按鈕時取消定時器。每個計時器操作應該刪除/退格。 – rmaddy

回答

0

我會通過在屏幕上添加一個按鈕而不添加目標來解決這個問題。然後我會使用觸摸事件來觸發按鈕動作。當您創建按鈕時,將標籤設置爲唯一值。在我的例子中,我將標記設置爲100.然後實現下面的代碼,你應該很好去。

- (void)backspace { 
    if (![textField.text isEqualToString:@""]) { 
     NSString *backspace = textField.text; 
     int lengthofstring = backspace.length; 
     backspace = [backspace substringToIndex:lengthofstring - 1]; 
     textField.text = backspace; 
    } 
} 

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    UITouch *touch = [touches anyObject]; 

    if (touch.view.tag == 100) { 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(backspace) userInfo:nil repeats:YES]; 
    } 
} 

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 
    UITouch *touch = [touches anyObject]; 

    if (touch.view.tag == 100) { 
     if (timer != nil) 
      [timer invalidate]; 
     timer = nil; 
    } 
} 

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    UITouch *touch = [touches anyObject]; 

    if (touch.view.tag == 100) { 
     if (timer != nil) { 
      [timer invalidate]; 
      timer = nil; 
     } 
    } 
}