uitextview删除文本按钮touchhold重复

时间:2012-10-20 21:04:29

标签: iphone uibutton uitextview ios5.1 backspace

我有一个可编辑的uitextview。我正在尝试添加一个退格删除按钮,模仿标准键盘退格/删除按钮operation.ie touchhold重复功能。

1 个答案:

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