自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)教程之手勢識(shí)別方法

移動(dòng)開發(fā) iOS
感覺有必要把iOS開發(fā)中的手勢識(shí)別做一個(gè)小小的總結(jié)。在上一篇iOS開發(fā)之自定義表情鍵盤(組件封裝與自動(dòng)布局)博客中用到了一個(gè)輕擊手勢,就是在輕擊TextView時(shí)從表情鍵盤回到系統(tǒng)鍵盤,在TextView中的手是用storyboard添加的。下面會(huì)先給出如何用storyboard給相應(yīng)的控件添加手勢,然后在用純代碼的方式給我們的控件添加手勢,手勢的用法比較簡單。和button的用法類似,

感覺有必要把iOS開發(fā)中的手勢識(shí)別做一個(gè)小小的總結(jié)。在上一篇iOS開發(fā)之自定義表情鍵盤(組件封裝與自動(dòng)布局)博客中用到了一個(gè)輕擊手勢,就是在輕擊TextView時(shí)從表情鍵盤回到系統(tǒng)鍵盤,在TextView中的手是用storyboard添加的。下面會(huì)先給出如何用storyboard給相應(yīng)的控件添加手勢,然后在用純代碼的方式給我們的控件添加手勢,手勢的用法比較簡單。和button的用法類似,也是目標(biāo)動(dòng)作回調(diào),話不多說,切入今天的正題??偣灿辛N手勢識(shí)別:輕擊手勢(TapGestureRecognizer),輕掃手勢(SwipeGestureRecognizer), 長按手勢(LongPressGestureRecognizer), 拖動(dòng)手勢(PanGestureRecognizer), 捏合手勢(PinchGestureRecognizer),旋轉(zhuǎn)手勢(RotationGestureRecognizer);

其實(shí)這些手勢用touche事件完全可以實(shí)現(xiàn),蘋果就是把常用的觸摸事件封裝成手勢,來提供給用戶。讀者完全可以用TouchesMoved來寫拖動(dòng)手勢等

一,用storyboard給控件添加手勢識(shí)別,當(dāng)然啦用storyboard得截張圖啦

1.用storyboard添加手勢識(shí)別,和添加一個(gè)Button的步驟一樣,首先我們得找到相應(yīng)的手勢,把手勢識(shí)別的控件拖到我們要添加手勢的控件中,截圖如下

2.給我們拖出的手勢添加回調(diào)事件,和給Button回調(diào)事件沒啥區(qū)別的,在回調(diào)方法中添加要實(shí)現(xiàn)的業(yè)務(wù)邏輯即可,截圖如下:

 二,純代碼添加手勢識(shí)別

用storyboard可以大大簡化我們的操作,不過純代碼的方式還是要會(huì)的,就像 要Dreamwear編輯網(wǎng)頁一樣(當(dāng)然啦,storyboard的拖拽功能要比Dreamwear的拖拽強(qiáng)大的多),用純代碼敲出來的更為靈活,更加便 于維護(hù)。不過用storyboard可以減少我們的工作量,這兩個(gè)要配合著使用才能大大的提高我們的開發(fā)效率。個(gè)人感覺用storyboard把框架搭起 來(Controller間的關(guān)系),一下小的東西還是用純代碼敲出來更好一些。下面就給出如何給我們的控件用純代碼的方式來添加手勢識(shí)別。

1.輕擊手勢(TapGestureRecognizer)的添加

初始化代碼TapGestureRecongnizer的代碼如下:

  1. 1     //新建tap手勢 
  2. 2     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; 
  3. 3     //設(shè)置點(diǎn)擊次數(shù)和點(diǎn)擊手指數(shù) 
  4. 4     tapGesture.numberOfTapsRequired = 1//點(diǎn)擊次數(shù) 
  5. 5     tapGesture.numberOfTouchesRequired = 1//點(diǎn)擊手指數(shù) 
  6. 6     [self.view addGestureRecognizer:tapGesture]; 

在回調(diào)方法中添加相應(yīng)的業(yè)務(wù)邏輯:

  1. 1 //輕擊手勢觸發(fā)方法 
  2. 2 -(void)tapGesture:(id)sender 
  3. 3 { 
  4. 4     //輕擊后要做的事情         
  5. 5 } 

2.長按手勢(LongPressGestureRecognizer)

初始化代碼:

  1. //添加長摁手勢 
  2. 2     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)]; 
  3. 3     //設(shè)置長按時(shí)間 
  4. 4     longPressGesture.minimumPressDuration = 0.5//(2秒) 
  5. 5     [self.view addGestureRecognizer:longPressGesture]; 

在對(duì)應(yīng)的回調(diào)方法中添加相應(yīng)的方法(當(dāng)手勢開始時(shí)執(zhí)行):

  1. 1 //常摁手勢觸發(fā)方法 
  2. 2 -(void)longPressGesture:(id)sender 
  3. 3 { 
  4. 4     UILongPressGestureRecognizer *longPress = sender; 
  5. 5     if (longPress.state == UIGestureRecognizerStateBegan) 
  6. 6     { 
  7. 7         UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"長按觸發(fā)" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil]; 
  8. 8         [alter show]; 
  9. 9     } 
  10. 0 } 

代碼說明:手勢的常用狀態(tài)如下

開始:UIGestureRecognizerStateBegan

改變:UIGestureRecognizerStateChanged

結(jié)束:UIGestureRecognizerStateEnded

取消:UIGestureRecognizerStateCancelled

失敗:UIGestureRecognizerStateFailed

3.輕掃手勢(SwipeGestureRecognizer)

在初始化輕掃手勢的時(shí)候得指定輕掃的方向,上下左右。 如果要要添加多個(gè)輕掃方向,就得添加多個(gè)輕掃手勢,不過回調(diào)的是同一個(gè)方法。

添加輕掃手勢,一個(gè)向左一個(gè)向右,代碼如下:

  1. 1     //添加輕掃手勢 
  2.  2     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; 
  3.  3     //設(shè)置輕掃的方向 
  4.  4     swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默認(rèn)向右 
  5.  5     [self.view addGestureRecognizer:swipeGesture]; 
  6.  6      
  7.  7     //添加輕掃手勢 
  8.  8     UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; 
  9.  9     //設(shè)置輕掃的方向 
  10. 10     swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默認(rèn)向右 
  11. 11     [self.view addGestureRecognizer:swipeGestureLeft]; 

回調(diào)方法如下:

  1. 1 //輕掃手勢觸發(fā)方法 
  2.  2 -(void)swipeGesture:(id)sender 
  3.  3 { 
  4.  4     UISwipeGestureRecognizer *swipe = sender; 
  5.  5     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) 
  6.  6     { 
  7.  7         //向左輕掃做的事情 
  8.  8     } 
  9.  9     if (swipe.direction == UISwipeGestureRecognizerDirectionRight) 
  10. 10     { 
  11. 11         //向右輕掃做的事情 
  12. 12     } 
  13. 13 } 
  14. 14      

4.捏合手勢(PinchGestureRecognizer)

捏合手勢初始化

  1.   //添加捏合手勢 
  2. 2     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; 
  3. 3     [self.view addGestureRecognizer:pinchGesture]; 

捏合手勢要觸發(fā)的方法(放大或者縮小圖片):

  1.  1 ////捏合手勢觸發(fā)方法 
  2.  2 -(void) pinchGesture:(id)sender 
  3.  3 { 
  4.  4      UIPinchGestureRecognizer *gesture = sender; 
  5.  5      
  6.  6     //手勢改變時(shí) 
  7.  7     if (gesture.state == UIGestureRecognizerStateChanged) 
  8.  8     { 
  9.  9         //捏合手勢中scale屬性記錄的縮放比例 
  10. 10         _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); 
  11. 11     } 
  12. 12      
  13. 13     //結(jié)束后恢復(fù) 
  14. 14     if(gesture.state==UIGestureRecognizerStateEnded) 
  15. 15     { 
  16. 16         [UIView animateWithDuration:0.5 animations:^{ 
  17. 17             _imageView.transform = CGAffineTransformIdentity;//取消一切形變 
  18. 18         }]; 
  19. 19     } 
  20. 20 } 

5.拖動(dòng)手勢(PanGestureRecognizer)

拖動(dòng)手勢的初始化

  1. //添加拖動(dòng)手勢 
  2. 2     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; 
  3. 3     [self.view addGestureRecognizer:panGesture]; 

拖動(dòng)手勢要做的方法(通過translationInView獲取移動(dòng)的點(diǎn),和TouchesMoved方法類似)

  1. 1 //拖動(dòng)手勢 
  2. 2 -(void) panGesture:(id)sender 
  3. 3 { 
  4. 4     UIPanGestureRecognizer *panGesture = sender; 
  5. 5      
  6. 6     CGPoint movePoint = [panGesture translationInView:self.view]; 
  7. 7      
  8. 8     //做你想做的事兒 
  9. 9 } 

6.旋轉(zhuǎn)手勢(RotationGestureRecognizer)

旋轉(zhuǎn)手勢的初始化

  1.  //添加旋轉(zhuǎn)手勢 
  2. 2     UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)]; 
  3. 3     [self.view addGestureRecognizer:rotationGesture]; 

旋轉(zhuǎn)手勢調(diào)用的方法:

  1. 復(fù)制代碼 
  2.  
  3.  1 //旋轉(zhuǎn)手勢 
  4.  2 -(void)rotationGesture:(id)sender 
  5.  3 { 
  6.  4      
  7.  5     UIRotationGestureRecognizer *gesture = sender; 
  8.  6      
  9.  7     if (gesture.state==UIGestureRecognizerStateChanged) 
  10.  8     { 
  11.  9         _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation); 
  12. 10     } 
  13. 11      
  14. 12     if(gesture.state==UIGestureRecognizerStateEnded) 
  15. 13     { 
  16. 14          
  17. 15         [UIView animateWithDuration:1 animations:^{ 
  18. 16             _imageView.transform=CGAffineTransformIdentity;//取消形變 
  19. 17         }]; 
  20. 18     } 
  21. 19      
  22. 20 } 

本文鏈接:http://www.cnblogs.com/ludashi/p/3983008.html

責(zé)任編輯:chenqingxiang 來源: cnblogs
相關(guān)推薦

2022-08-16 15:20:12

微服務(wù)IT運(yùn)維

2011-06-30 10:36:22

JSF

2011-06-30 10:49:27

2022-05-17 12:25:59

物聯(lián)網(wǎng)智能建筑樓宇自控

2018-04-02 10:45:11

深度學(xué)習(xí)PaddlePaddl手寫數(shù)字識(shí)別

2020-04-10 14:10:50

人臉識(shí)別人工智能華為

2024-06-20 11:11:07

2021-08-13 10:01:19

人臉識(shí)別人工智能數(shù)據(jù)

2009-06-11 19:54:19

Hibernate入門

2011-07-08 14:58:16

iPhone Xcode iOS

2013-05-21 11:20:37

Android游戲開發(fā)View手勢識(shí)別

2013-05-07 17:21:09

ELMOS芯片手勢識(shí)別

2022-03-28 07:52:31

H5小游戲開發(fā)教程頁面基礎(chǔ)布局

2009-07-17 09:44:40

iBATIS教程

2011-07-12 17:11:13

PHPSHELL

2013-06-20 10:50:51

Objective-CiOS左右滑動(dòng)手勢

2022-03-24 08:33:58

小游戲項(xiàng)目cmdvue3

2011-12-30 15:17:23

Adobe視頻PhoneGap

2013-05-02 11:21:36

iOS開發(fā)流程

2011-07-22 18:13:59

IOS IDE Xcode
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)