最近在項目中用到了手勢操作,鍵盤迴收時還是挺常用的,現在總結下,多謝網路上大神們的分享。 先分享下我在項目中用的代碼: 將相應代碼複製到你的工程中即可使用,由於代碼中已經有詳細的解釋說明,這裡就不在重覆解釋了。 代碼中只是列舉了單指與雙指對於單擊或多擊的處理,同理多指的操作需修改numberOfTo ...
最近在項目中用到了手勢操作,鍵盤迴收時還是挺常用的,現在總結下,多謝網路上大神們的分享。
先分享下我在項目中用的代碼:
UITapGestureRecognizer * mytap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap_gestureRecognizer:)]; [self addGestureRecognizer:mytap]; [mytap release]; //收回鍵盤 -(void)tap_gestureRecognizer:(UITapGestureRecognizer *)tap_gest { [self.scv_stockPriceTextField resignFirstResponder]; [self.scv_stockAmoutTextField resignFirstResponder]; }
//單指單擊 UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)]; singleFingerOne.numberOfTouchesRequired = 1; //手指數 singleFingerOne.numberOfTapsRequired = 1; //tap次數 singleFingerOne.delegate = self; //單指雙擊 UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)]; singleFingerTwo.numberOfTouchesRequired = 1; singleFingerTwo.numberOfTapsRequired = 2; singleFingerTwo.delegate = self; //雙指單擊 UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)]; doubleFingerOne.numberOfTouchesRequired = 2; doubleFingerOne.numberOfTapsRequired = 1; doubleFingerOne.delegate = self; UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)]; doubleFingerTwo.numberOfTouchesRequired = 2; doubleFingerTwo.numberOfTapsRequired = 2; doubleFingerTwo.delegate = self; //如果不加下麵的話,當單指雙擊時,會先調用單指單擊中的處理,再調用單指雙擊中的處理 [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo]; //同理雙指亦是如此 [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo]; [self.view addGestureRecognizer:singleFingerOne]; [self.view addGestureRecognizer:singleFingerTwo]; [self.view addGestureRecognizer:doubleFingerOne]; [self.view addGestureRecognizer:doubleFingerTwo]; [singleFingerOne release]; [singleFingerTwo release]; [doubleFingerOne release]; [doubleFingerTwo release];
處理事件的方法,代碼: 1: //處理單指事件 2: - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender 3: { 4: if (sender.numberOfTapsRequired == 1) { 5: //單指單擊 6: NSLog(@"單指單擊"); 7: }else if(sender.numberOfTapsRequired == 2){ 8: //單指雙擊 9: NSLog(@"單指雙擊"); 10: } 11: } 12: //處理雙指事件 13: - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender 14: { 15: if (sender.numberOfTapsRequired == 1) { 16: //雙指單擊 17: NSLog(@"雙指單擊"); 18: }else if(sender.numberOfTapsRequired == 2){ 19: //雙指雙擊 20: NSLog(@"雙指雙擊"); 21: } 22: }
將相應代碼複製到你的工程中即可使用,由於代碼中已經有詳細的解釋說明,這裡就不在重覆解釋了。
代碼中只是列舉了單指與雙指對於單擊或多擊的處理,同理多指的操作需修改numberOfTouchesRequired屬性,對點擊的次數需修改numberOfTapsRequired屬性。
對於其他手勢例如UISwipeGestureRecognizer,UILongPressGestureRecognizer,UILongPressGestureRecognizer 的操作使用類似處理。
UIKit中包含了UIGestureRecognizer類,用於檢測發生在設備中的手勢。UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,它有下麵一些子類用於處理具體的手勢:
1、拍擊UITapGestureRecognizer (任意次數的拍擊)
2、向里或向外捏UIPinchGestureRecognizer (用於縮放)
3、搖動或者拖拽UIPanGestureRecognizer
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋轉UIRotationGestureRecognizer (手指朝相反方向移動)
6、長按UILongPressGestureRecognizer
對於不同類型的手勢識別器,具有不同的配置屬性。比如UITapGestureRecognizer,可以配置拍擊次數。界面接收到手勢之後,可以發送一 個消息,用於處理響應手勢動作後的任務。當然,不同的手勢識別器,發送的消息方法也會有所不同。