Это было весело.
По сути, я написал метод для извлечения последнего символа из текстовой строки textField.
Я добавил еще один метод для вызова события touchDown Button, которое сначала вызывает метод для удаления последнего символа, а затем запускает таймер для начала повторения. Поскольку задержка до повторения (по крайней мере на родной клавиатуре) больше, чем задержка повторения, я использую два таймера. Первый вариант повтора установлен на NO. он вызывает метод, который запускает второй таймер, который повторяется для повторного вызова метода стирания последнего символа.
В дополнение к событию touchDown. Мы также регистрируемся на событие touchUpInside. При срабатывании вызывает метод, который делает недействительным текущий таймер.
#import <UIKit/UIKit.h>
#define kBackSpaceRepeatDelay 0.1f
#define kBackSpacePauseLengthBeforeRepeting 0.2f
@interface clearontapAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSTimer *repeatBackspaceTimer;
UITextField *textField;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSTimer *repeatBackspaceTimer;
@end
@implementation clearontapAppDelegate
@synthesize window,
@synthesize repeatBackspaceTimer;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.text = @"hello world........";
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 80, 300, 30);
button.backgroundColor = [UIColor redColor];
button.titleLabel.text = @"CLEAR";
[button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
// Override point for customization after application launch
[window addSubview:textField];
[window addSubview:button];
window.backgroundColor = [UIColor blueColor];
[window makeKeyAndVisible];
}
-(void) eraseLastLetter:(id)sender {
if (textField.text.length > 0) {
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
}
-(void) startRepeating:(id)sender {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kBackSpaceRepeatDelay
target:self selector:@selector(eraseLastLetter:)
userInfo:nil repeats:YES];
self.repeatBackspaceTimer = timer;
}
-(void) touchDown:(id)sender {
[self eraseLastLetter:self];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kBackSpacePauseLengthBeforeRepeting
target:self selector:@selector(startRepeating:)
userInfo:nil repeats:YES];
self.repeatBackspaceTimer = timer;
}
-(void) touchUpInside:(id)sender {
[self.repeatBackspaceTimer invalidate];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end