上圖所示,爲項目設計圖,即在文本框中輸入序列號,右側爲刪除按鈕,可刪除文本框中的文字。設計
本文記錄的知識要點在於,長按刪除按鈕,文本框中的文字會逐個刪除。blog
因爲時間倉促,來不及詳細記錄過程,直接上代碼:get
因爲是長按刪除按鈕引起的操做,操做確定要針對按鈕!it
對刪除按鈕的操做:io
a、添加長按手勢date
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(reduceText:)];select
[self.deleteButton addGestureRecognizer:press];方法
b、手勢出發的方法im
- (void)reduceText:(UILongPressGestureRecognizer *)gesture{項目
if (gesture.state == UIGestureRecognizerStateBegan) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(deleteText) userInfo:nil repeats:YES];
}else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled){
[_timer invalidate];
_timer = nil;
}
}