iOS開發之手勢識別

來源:互聯網
上載者:User

標籤:

感覺有必要把iOS開發中的手勢識別做一個小小的總結。在上一篇iOS開發之自訂表格情鍵盤(組件封裝與自動布局)部落格中用到了一個輕擊手勢,就是在輕擊TextView時從表情鍵盤迴到系統鍵盤,在TextView中的手是用storyboard添加的。下面會先給出如何用storyboard給相應的控制項添加手勢,然後在用純程式碼的方式給我們的控制項添加手勢,手勢的用法比較簡單。和button的用法類似,也是目標動作回調,話不多說,切入今天的正題。總共有六種手勢識別:輕擊手勢(TapGestureRecognizer),撥動手勢(SwipeGestureRecognizer), 長按手勢(LongPressGestureRecognizer),  拖動手勢(PanGestureRecognizer), 捏合手勢(PinchGestureRecognizer),旋轉手勢(RotationGestureRecognizer);

  其實這些手勢用touche事件完全可以實現,蘋果就是把常用的觸摸事件封裝成手勢,來提供給使用者。讀者完全可以用TouchesMoved來寫拖動手勢等

  一,用storyboard給控制項添加手勢識別,當然啦用storyboard得截張圖啦

    1.用storyboard添加手勢識別,和添加一個Button的步驟一樣,首先我們得找到相應的手勢,把手勢識別的控制項拖到我們要添加手勢的控制項中,如下:


    2.給我們拖出的手勢添加回調事件,和給Button回調事件沒啥區別的,在回調方法中添加要實現的商務邏輯即可,如下:

 

  

  二,純程式碼添加手勢識別

    用storyboard可以大大簡化我們的操作,不過純程式碼的方式還是要會的,就像要Dreamwear編輯網頁一樣(當然啦,storyboard的拖拽功能要比Dreamwear的拖拽強大的多),用純程式碼敲出來的更為靈活,更加便於維護。不過用storyboard可以減少我們的工作量,這兩個要配合著使用才能大大的提高我們的開發效率。個人感覺用storyboard把架構搭起來(Controller間的關係),一下小的東西還是用純程式碼敲出來更好一些。下面就給出如何給我們的控制項用純程式碼的方式來添加手勢識別。

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

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

1     //建立tap手勢2     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];3     //設定點擊次數和點擊手指數4     tapGesture.numberOfTapsRequired = 1; //點擊次數5     tapGesture.numberOfTouchesRequired = 1; //點擊手指數6     [self.view addGestureRecognizer:tapGesture];

    在回調方法中添加相應的商務邏輯:

1 //輕擊手勢觸發方法2 -(void)tapGesture:(id)sender3 {4     //輕擊後要做的事情        5 }

 

    2.長按手勢(LongPressGestureRecognizer)

      初始化代碼:

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

     在對應的回調方法中添加相應的方法(當手勢開始時執行):

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

    代碼說明:手勢的常用狀態如下

      開始:UIGestureRecognizerStateBegan

      改變:UIGestureRecognizerStateChanged

      結束:UIGestureRecognizerStateEnded

        取消:UIGestureRecognizerStateCancelled

      失敗:UIGestureRecognizerStateFailed

 

    3.撥動手勢(SwipeGestureRecognizer)

      在初始化撥動手勢的時候得指定輕掃的方向,上下左右。 如果要要添加多個輕掃方向,就得添加多個撥動手勢,不過回調的是同一個方法。

      添加撥動手勢,一個向左一個向右,代碼如下:

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

     回調方法如下:

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

 

    4.捏合手勢(PinchGestureRecognizer)

      捏合手勢初始化

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

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

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

 

    5.拖動手勢(PanGestureRecognizer)

      拖動手勢的初始化

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

    拖動手勢要做的方法(通過translationInView擷取移動的點,和TouchesMoved方法類似)

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

  

    6.旋轉手勢(RotationGestureRecognizer)

      旋轉手勢的初始化

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

      旋轉手勢調用的方法:

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

  

  上面的東西沒有多高深的技術,就是對iOS開發中的手勢做了一下小小的總結,溫故一下基礎知識。在之前的部落格中也有用到手勢識別的內容,就是沒有系統的梳理一下手勢識別的知識,本篇部落格做一個基礎的補充吧。歡迎批評指正,轉載請註明出處。

iOS開發之手勢識別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.