0

我有一个可编辑的uitextview。我正在尝试添加一个模仿标准键盘退格/删除按钮操作的退格删除按钮。即触摸重复功能。

4

1 回答 1

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;
        }
    }
}
于 2012-10-21T03:34:58.013 回答