iOS開發(fā)之手勢(shì)識(shí)別實(shí)例
感覺有必要把iOS開發(fā)中的手勢(shì)識(shí)別做一個(gè)小小的總結(jié)。下面會(huì)先給出如何用storyboard給相應(yīng)的控件添加手勢(shì),然后在用純代碼的方式給我們的控件添加手勢(shì),手勢(shì)的用法比較簡(jiǎn)單。和button的用法類似,也是目標(biāo) 動(dòng)作回調(diào),話不多說,切入今天的正題。
總共有六種手勢(shì)識(shí)別:輕擊手勢(shì)(TapGestureRecognizer),輕掃手勢(shì) (SwipeGestureRecognizer), 長按手勢(shì)(LongPressGestureRecognizer), 拖動(dòng)手勢(shì)(PanGestureRecognizer), 捏合手勢(shì)(PinchGestureRecognizer),旋轉(zhuǎn)手勢(shì)(RotationGestureRecognizer);
其實(shí)這些手勢(shì)用touche事件完全可以實(shí)現(xiàn),蘋果就是把常用的觸摸事件封裝成手勢(shì),來提供給用戶。讀者完全可以用TouchesMoved來寫拖動(dòng)手勢(shì)等
一,用storyboard給控件添加手勢(shì)識(shí)別
1.用storyboard添加手勢(shì)識(shí)別,和添加一個(gè)Button的步驟一樣,首先我們得找到相應(yīng)的手勢(shì),把手勢(shì)識(shí)別的控件拖到我們要添加手勢(shì)的控件中,截圖如下:
2.給我們拖出的手勢(shì)添加回調(diào)事件,和給Button回調(diào)事件沒啥區(qū)別的,在回調(diào)方法中添加要實(shí)現(xiàn)的業(yè)務(wù)邏輯即可,截圖如下:
二,純代碼添加手勢(shì)識(shí)別
用storyboard可以大大簡(jiǎn)化我們的操作,不過純代碼的方式還是要會(huì)的,就像要Dreamwear編輯網(wǎng)頁一樣(當(dāng)然啦,storyboard的拖拽功能要比Dreamwear的拖拽強(qiáng)大的多),用純代碼敲出來的更為靈活,更加便 于維護(hù)。不過用storyboard可以減少我們的工作量,這兩個(gè)要配合著使用才能大大的提高我們的開發(fā)效率。個(gè)人感覺用storyboard把框架搭起 來(Controller間的關(guān)系),一下小的東西還是用純代碼敲出來更好一些。下面就給出如何給我們的控件用純代碼的方式來添加手勢(shì)識(shí)別。
1.輕擊手勢(shì)(TapGestureRecognizer)的添加
初始化代碼TapGestureRecongnizer的代碼如下:
//新建tap手勢(shì) UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; //設(shè)置點(diǎn)擊次數(shù)和點(diǎn)擊手指數(shù) tapGesture.numberOfTapsRequired = 1; //點(diǎn)擊次數(shù) tapGesture.numberOfTouchesRequired = 1; //點(diǎn)擊手指數(shù) [self.view addGestureRecognizer:tapGesture];
在回調(diào)方法中添加相應(yīng)的業(yè)務(wù)邏輯:
//輕擊手勢(shì)觸發(fā)方法 -(void)tapGesture:(id)sender { //輕擊后要做的事情 }
2.長按手勢(shì)(LongPressGestureRecognizer)
初始化代碼:
//添加長摁手勢(shì) UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)]; //設(shè)置長按時(shí)間 longPressGesture.minimumPressDuration = 0.5; //(2秒) [self.view addGestureRecognizer:longPressGesture];
在對(duì)應(yīng)的回調(diào)方法中添加相應(yīng)的方法(當(dāng)手勢(shì)開始時(shí)執(zhí)行):
//常摁手勢(shì)觸發(fā)方法 -(void)longPressGesture:(id)sender { UILongPressGestureRecognizer *longPress = sender; if (longPress.state == UIGestureRecognizerStateBegan) { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"長按觸發(fā)" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil]; [alter show]; } }
代碼說明:手勢(shì)的常用狀態(tài)如下
- 開始:UIGestureRecognizerStateBegan
- 改變:UIGestureRecognizerStateChanged
- 結(jié)束:UIGestureRecognizerStateEnded
- 取消:UIGestureRecognizerStateCancelled
- 失?。篣IGestureRecognizerStateFailed
3.輕掃手勢(shì)(SwipeGestureRecognizer)
在初始化輕掃手勢(shì)的時(shí)候得指定輕掃的方向,上下左右。 如果要要添加多個(gè)輕掃方向,就得添加多個(gè)輕掃手勢(shì),不過回調(diào)的是同一個(gè)方法。
添加輕掃手勢(shì),一個(gè)向左一個(gè)向右,代碼如下:
//添加輕掃手勢(shì) UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //設(shè)置輕掃的方向 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默認(rèn)向右 [self.view addGestureRecognizer:swipeGesture]; //添加輕掃手勢(shì) UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //設(shè)置輕掃的方向 swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默認(rèn)向右 [self.view addGestureRecognizer:swipeGestureLeft];
回調(diào)方法如下:
//輕掃手勢(shì)觸發(fā)方法 -(void)swipeGesture:(id)sender { UISwipeGestureRecognizer *swipe = sender; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //向左輕掃做的事情 } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { //向右輕掃做的事情 } }
4.捏合手勢(shì)(PinchGestureRecognizer)
捏合手勢(shì)初始化
//添加捏合手勢(shì) UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; [self.view addGestureRecognizer:pinchGesture];
捏合手勢(shì)要觸發(fā)的方法(放大或者縮小圖片):
////捏合手勢(shì)觸發(fā)方法 -(void) pinchGesture:(id)sender { UIPinchGestureRecognizer *gesture = sender; //手勢(shì)改變時(shí) if (gesture.state == UIGestureRecognizerStateChanged) { //捏合手勢(shì)中scale屬性記錄的縮放比例 _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); } //結(jié)束后恢復(fù) if(gesture.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:0.5 animations:^{ _imageView.transform = CGAffineTransformIdentity;//取消一切形變 }]; } }
5.拖動(dòng)手勢(shì)(PanGestureRecognizer)
拖動(dòng)手勢(shì)的初始化
//添加拖動(dòng)手勢(shì) UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; [self.view addGestureRecognizer:panGesture];
拖動(dòng)手勢(shì)要做的方法(通過translationInView獲取移動(dòng)的點(diǎn),和TouchesMoved方法類似)
//拖動(dòng)手勢(shì) -(void) panGesture:(id)sender { UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //做你想做的事兒 }
6.旋轉(zhuǎn)手勢(shì)(RotationGestureRecognizer)
旋轉(zhuǎn)手勢(shì)的初始化
//添加旋轉(zhuǎn)手勢(shì) UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)]; [self.view addGestureRecognizer:rotationGesture];
旋轉(zhuǎn)手勢(shì)調(diào)用的方法:
//旋轉(zhuǎn)手勢(shì) -(void)rotationGesture:(id)sender { UIRotationGestureRecognizer *gesture = sender; if (gesture.state==UIGestureRecognizerStateChanged) { _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation); } if(gesture.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:1 animations:^{ _imageView.transform=CGAffineTransformIdentity;//取消形變 }]; } }
上面的東西沒有多高深的技術(shù),就是對(duì)iOS開發(fā)中的手勢(shì)做了一下小小的總結(jié),溫故一下基礎(chǔ)知識(shí)。在之前的博客中也有用到手勢(shì)識(shí)別的內(nèi)容,就是沒有系統(tǒng)的梳理一下手勢(shì)識(shí)別的知識(shí),本文做一個(gè)基礎(chǔ)的補(bǔ)充吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ios實(shí)現(xiàn)UITableView之間圓角和間隙
這篇文章主要為大家詳細(xì)介紹了ios實(shí)現(xiàn)UITableView之間圓角和間隙,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08iOS開發(fā)中使用UIScrollView實(shí)現(xiàn)圖片輪播和點(diǎn)擊加載
這篇文章主要介紹了iOS開發(fā)中使用UIScrollView實(shí)現(xiàn)圖片輪播和點(diǎn)擊加載的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12IOS關(guān)于大型網(wǎng)站搶購、距活動(dòng)結(jié)束,剩余時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS關(guān)于大型網(wǎng)站搶購、距活動(dòng)結(jié)束,剩余時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08VIVO手機(jī)上del鍵無效OnKeyListener不響應(yīng)的原因及解決方法
最近有用戶反饋VIVO手機(jī)上回出現(xiàn),Del鍵無效的問題,最后找到問題所在是EdiText的OnKeyListener沒有響應(yīng),下面通過本文給大家分享下解決方案2016-12-12iOS常見算法以及應(yīng)用知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于iOS常見算法以及應(yīng)用知識(shí)點(diǎn)總結(jié),有興趣的朋友們學(xué)習(xí)下。2019-10-10IOS 上架后出現(xiàn)90034代碼錯(cuò)誤問題解決
這篇文章主要介紹了IOS 上架后出現(xiàn)90034代碼錯(cuò)誤問題解決的相關(guān)資料,需要的朋友可以參考下2016-11-11iOS整個(gè)APP實(shí)現(xiàn)灰色主題的示例代碼
這篇文章主要介紹了iOS整個(gè)APP實(shí)現(xiàn)灰色主題的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲)
本篇文章主要介紹了iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01快速解決低版本Xcode不支持高版本iOS真機(jī)調(diào)試的問題方法
這篇文章主要介紹了快速解決低版本Xcode不支持高版本iOS真機(jī)調(diào)試的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02