我覺得有必要把 iOS 開發中的手勢辨識做個小整理。下面會先示範如何用 Storyboard 為控件加入手勢,再說明如何以純程式碼的方式為控件添加手勢。手勢的用法相當簡單,和 Button 類似,同樣是「目標—動作—回呼」的模式,話不多說,直接進入主題。
總共有六種手勢辨識:輕點手勢(TapGestureRecognizer)、輕掃手勢(SwipeGestureRecognizer)、長按手勢(LongPressGestureRecognizer)、拖曳手勢(PanGestureRecognizer)、捏合手勢(PinchGestureRecognizer)、旋轉手勢(RotationGestureRecognizer)。
其實這些手勢用 touches 事件都能自行實作,Apple 只是把常用的觸控事件封裝成手勢提供給開發者。讀者完全可以用 TouchesMoved 來打造拖曳手勢等效果。
一、用 Storyboard 為控件加入手勢辨識
1. 使用 Storyboard 加入手勢的步驟和加入一個 Button 幾乎相同,首先要把對應的手勢控件拖到需要加入手勢的控件上,如下圖:
2. 為拖出的手勢加上回呼事件,這和為 Button 加上回呼沒有太大差別,在回呼方法裡寫入要實作的業務邏輯即可,如下圖:
二、純程式碼加入手勢辨識
用 Storyboard 能大幅簡化操作,但純程式碼的方式同樣必須掌握。純程式碼敲出來的更為靈活、也更易於維護;而 Storyboard 則能減少工作量,兩者搭配使用才能大幅提升開發效率。個人偏好用 Storyboard 把架構(各 Controller 之間的關係)搭起來,瑣碎的細節則用純程式碼實作,效果往往更好。以下就示範如何以純程式碼為控件加入手勢辨識。
1. 輕點手勢(TapGestureRecognizer)
初始化程式碼如下:
//新建tap手勢 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; //設置點擊次數和點擊手指數 tapGesture.numberOfTapsRequired = 1; //點擊次數 tapGesture.numberOfTouchesRequired = 1; //點擊手指數 [self.view addGestureRecognizer:tapGesture];
在回呼方法中加入相應的業務邏輯:
//輕擊手勢觸發方法 -(void)tapGesture:(id)sender { //輕擊後要做的事情 }
2. 長按手勢(LongPressGestureRecognizer)
初始化程式碼:
//添加長摁手勢 UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)]; //設置長按時間 longPressGesture.minimumPressDuration = 0.5; //(2秒) [self.view addGestureRecognizer:longPressGesture];
在對應的回呼方法中實作(當手勢開始時執行):
//常摁手勢觸發方法 -(void)longPressGesture:(id)sender { UILongPressGestureRecognizer *longPress = sender; if (longPress.state == UIGestureRecognizerStateBegan) { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"長按觸發" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil]; [alter show]; } }
程式碼說明:手勢的常用狀態如下
3. 輕掃手勢(SwipeGestureRecognizer)
初始化輕掃手勢時必須指定輕掃方向(上下左右)。若要加入多個輕掃方向,就得建立多個輕掃手勢,不過可以指向同一個回呼方法。
加入向左與向右兩個輕掃手勢的程式碼如下:
//添加輕掃手勢 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //設置輕掃的方向 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默認向右 [self.view addGestureRecognizer:swipeGesture]; //添加輕掃手勢 UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //設置輕掃的方向 swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默認向右 [self.view addGestureRecognizer:swipeGestureLeft];
回呼方法如下:
//輕掃手勢觸發方法 -(void)swipeGesture:(id)sender { UISwipeGestureRecognizer *swipe = sender; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //向左輕掃做的事情 } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { //向右輕掃做的事情 } }
4. 捏合手勢(PinchGestureRecognizer)
捏合手勢初始化:
//添加捏合手勢 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)]; [self.view addGestureRecognizer:pinchGesture];
捏合手勢要觸發的方法(放大或縮小圖片):
////捏合手勢觸發方法 -(void) pinchGesture:(id)sender { UIPinchGestureRecognizer *gesture = sender; //手勢改變時 if (gesture.state == UIGestureRecognizerStateChanged) { //捏合手勢中scale屬性記錄的縮放比例 _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); } //結束後恢復 if(gesture.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:0.5 animations:^{ _imageView.transform = CGAffineTransformIdentity;//取消一切形變 }]; } }
5. 拖曳手勢(PanGestureRecognizer)
拖曳手勢的初始化:
//添加拖動手勢 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; [self.view addGestureRecognizer:panGesture];
拖曳手勢要實作的方法(透過 translationInView 取得移動的點,用法與 TouchesMoved 類似):
//拖動手勢 -(void) panGesture:(id)sender { UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //做你想做的事兒 }
6. 旋轉手勢(RotationGestureRecognizer)
旋轉手勢的初始化:
//添加旋轉手勢 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)]; [self.view addGestureRecognizer:rotationGesture];
旋轉手勢呼叫的方法:
//旋轉手勢 -(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;//取消形變 }]; } }
上面這些內容並沒有太高深的技術,只是對 iOS 開發中的手勢做個小結、複習一下基礎。之前的文章也曾用到手勢辨識,只是沒有系統性地梳理,本文就當作一個基礎補充。
以上就是本文的完整內容,希望對你的學習有所幫助,也感謝你的支持。