在 iOS 的開發過程中,我們一般使用 touches 方法來監聽 view 的觸控事件,但是這樣使用會有一些弊端:
- 必須得自定義 view, 在自定義的 View 當中去實現 touches 方法
- 由於是在 view 內部的 touches 方法中監聽觸控事件,因此預設情況下,無法讓其他外界物件監聽 view 的觸控事件
- 不容易區分使用者具體的手勢行為
鑑於這些問題,在iOS 3.2 之後,蘋果推出了手勢識別功能(Gesture Recognizer)在觸控事件處理方面大大簡化了開發者的開發難度。
UIGestureRecognizer 手勢識別器
- 利用 UIGestureRecognizer,能輕鬆識別使用者在某個 view 上面做的一些常見手勢
- UIGestureRecognizer 是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢
- 點按手勢
UITapGestureRecognizer
- 長按手勢
UILongPressGestureRecognizer
- 平移(拖拽)手勢
UIPanGestureRecognizer
- 輕掃手勢
UISwipeGestureRecognizer
- 旋轉手勢
UIRotationGestureRecognizer
- 捏合手勢
UIPinchGestureRecognizer
- 點按手勢
- 手勢的使用方法
- 建立手勢
- 新增手勢
- 實現手勢方法
- 補充(手勢也可以設定代理)
1. UITapGestureRecognizer 點按手勢
點按手勢效果圖
1 2 3 4 5 6 7 8 9 10 11 12 |
- (void)topGes { //建立點按手勢 UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]; //手勢也可以設定代理 tapGes.delegate = self; //新增手勢 [self.imageView addGestureRecognizer:tapGes]; } - (void)tap { NSLog(@"現在是點按手勢"); } |
- 實現代理方法
1 2 3 4 5 6 7 8 9 10 11 |
//代理方法:是否允許接收手指 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { //讓圖片左邊可以點選,右邊不能點選 //獲取當前手指所在的點進行判斷 CGPoint curP = [touch locationInView:self.imageView]; if (curP.x > self.imageView.bounds.size.width * 0.5) { return NO; }else { return YES; } } |
2. UILongPressGestureRecognizer 長按手勢
長按手勢效果圖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- (void)longPGes { UILongPressGestureRecognizer *longPGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longP:)]; [self.imageView addGestureRecognizer:longPGes]; } //當長按並移動時,會多次呼叫這個方法 - (void)longP:(UILongPressGestureRecognizer *)longP { //判斷長按時的狀態 if (longP.state == UIGestureRecognizerStateBegan) { NSLog(@"開始長按"); }else if (longP.state == UIGestureRecognizerStateChanged) { NSLog(@"- - - 長按時手指移動了 - - -"); }else if (longP.state == UIGestureRecognizerStateEnded) { NSLog(@"長按結束"); } } |
3. UIPanGestureRecognizer 平移(拖拽)手勢
平移(拖拽)手勢效果圖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- (void)panGes { UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.imageView addGestureRecognizer:panGes]; } - (void)pan:(UIPanGestureRecognizer *)pan { //獲取偏移量 CGPoint transP = [pan translationInView:self.imageView]; NSLog(@"transP = %@", NSStringFromCGPoint(transP)); //移動圖片 self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y); //清零,不要累加 [pan setTranslation:CGPointZero inView:self.imageView]; } |
4. UISwipeGestureRecognizer 輕掃手勢
輕掃手勢效果圖
- 一個手勢只能對應一個方向,若要支援多個方向,可以新增多個手勢
- 輕掃手勢的方向預設是向右
- 可以通過修改手勢的 direction 屬性修改手勢的方向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- (void)swipeGes { //一個手勢只能對應一個方向,若要支援多個方向,可以新增多個手勢 UISwipeGestureRecognizer *swipeGes1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; //輕掃手勢的方向預設是向右 [self.imageView addGestureRecognizer:swipeGes1]; UISwipeGestureRecognizer *swipeGes2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; //可以修改手勢的方向,向上 swipeGes2.direction = UISwipeGestureRecognizerDirectionUp; [self.imageView addGestureRecognizer:swipeGes2]; UISwipeGestureRecognizer *swipeGes3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; //手勢的方向,向左 swipeGes3.direction = UISwipeGestureRecognizerDirectionLeft; [self.imageView addGestureRecognizer:swipeGes3]; UISwipeGestureRecognizer *swipeGes4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; //手勢的方向,向下 swipeGes4.direction = UISwipeGestureRecognizerDirectionDown; [self.imageView addGestureRecognizer:swipeGes4]; } - (void)swipe:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"向左輕掃 --- left"); }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){ NSLog(@"向右輕掃 --- right"); }else if (swipe.direction == UISwipeGestureRecognizerDirectionUp){ NSLog(@"向上輕掃 --- up"); } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"向下輕掃 --- down"); } } |
5. UIRotationGestureRecognizer 旋轉手勢
旋轉手勢效果圖
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- (void)rotationGes { UIRotationGestureRecognizer *rotationGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; [self.imageView addGestureRecognizer:rotationGes]; } - (void)rotation:(UIRotationGestureRecognizer *)rotationGes { //獲取旋轉的度數 CGFloat angle = rotationGes.rotation; //旋轉圖片 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, angle); //清零 [rotationGes setRotation:0]; } |
6. UIPinchGestureRecognizer 捏合手勢
捏合手勢效果圖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (void)pinchGes { UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [self.imageView addGestureRecognizer:pinchGes]; } - (void)pinch:(UIPinchGestureRecognizer *)pinchGes { //獲取縮放比例 CGFloat scale = pinchGes.scale; NSLog(@"scale = %f", scale); //縮放圖片 self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale); //初始化大小 [pinchGes setScale:1]; } |
補充:手勢的混合使用
- 預設情況下只能同時支援一種手勢
- 若要同時支援多種手勢,可以給要支援的手勢設定代理,並實現以下代理方法
1 2 3 4 5 |
//是否允許同時支援多個手勢 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } |
旋轉與捏合混合手勢使用效果圖
至此,iOS 開發中的 6 種手勢操作已經介紹完畢了,大家可以通過設定多種手勢混合使用來達到一定的效果,希望大家都能更上一層樓!