iOS手勢操作:拖動、捏合、旋轉、點按、長按、輕掃、自定義
作者:網絡轉載 發布時間:[ 2015/11/13 14:31:17 ] 推薦標簽:移動測試 移動開發
1、UIGestureRecognizer 介紹
手勢識別在 iOS 中非常重要,他極大地提高了移動設備的使用便捷性。
iOS 系統在 3.2 以后,他提供了一些常用的手勢(UIGestureRecognizer 的子類),開發者可以直接使用他們進行手勢操作。
UIPanGestureRecognizer(拖動)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋轉)
UITapGestureRecognizer(點按)
UILongPressGestureRecognizer(長按)
UISwipeGestureRecognizer(輕掃)
另外,可以通過繼承 UIGestureRecognizer 類,實現自定義手勢(手勢識別器類)。
PS:自定義手勢時,需要 #import <UIKit/UIGestureRecognizerSubclass.h>
,一般需實現如下方法:
UIGestureRecognizer 的繼承關系如下:
1 - (void)reset;
2
3 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
4 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
5 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
6 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
7 //以上方法在分類 UIGestureRecognizer (UIGestureRecognizerProtected) 中聲明,更多方法聲明請自行查看
2、手勢狀態
在六種手勢識別中,只有一種手勢是離散型手勢,他是 UITapGestureRecognizer。
離散型手勢的特點是:一旦識別無法取消,而且只會調用一次手勢操作事件(初始化手勢時指定的回調方法)。
換句話說其他五種手勢是連續型手勢,而連續型手勢的特點是:會多次調用手勢操作事件,而且在連續手勢識別后可以取消手勢。從下圖可以看出兩者調用操作事件的次數是不同的:
手勢狀態枚舉如下:
1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { 2 UIGestureRecognizerStatePossible, // 尚未識別是何種手勢操作(但可能已經觸發了觸摸事件),默認狀態 3 UIGestureRecognizerStateBegan, // 手勢已經開始,此時已經被識別,但是這個過程中可能發生變化,手勢操作尚未完成 4 UIGestureRecognizerStateChanged, // 手勢狀態發生轉變 5 UIGestureRecognizerStateEnded, // 手勢識別操作完成(此時已經松開手指) 6 UIGestureRecognizerStateCancelled, // 手勢被取消,恢復到默認狀態 7 UIGestureRecognizerStateFailed, // 手勢識別失敗,恢復到默認狀態 8 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手勢識別完成,同UIGestureRecognizerStateEnded 9 };
對于離散型手勢 UITapGestureRecgnizer 要么被識別,要么失敗,點按(假設點按次數設置為1,并且沒有添加長按手勢)下去一次不松開則此時什么也不會發生,松開手指立即識別并調用操作事件,并且狀態為3(已完成)。
但是連續型手勢要復雜一些,拿旋轉手勢來說,如果兩個手指點下去不做任何操作,此時并不能識別手勢(因為我們還沒旋轉)但是其實已經觸發了觸摸開始事件,此時處于狀態0;如果此時旋轉會被識別,也會調用對應的操作事件,同時狀態變成1(手勢開始),但是狀態1只有一瞬間;緊接著狀態變為2(因為我們的旋轉需要持續一會),并且重復調用操作事件(如果在事件中打印狀態會重復打印2);松開手指,此時狀態變為3,并調用1次操作事件。
3、使用手勢的步驟
使用手勢很簡單,分為三步:
創建手勢識別器對象實例。創建時,指定一個回調方法,當手勢開始,改變、或結束時,執行回調方法。
設置手勢識別器對象實例的相關屬性(可選部分)
添加到需要識別的 View 中。每個手勢只對應一個 View,當屏幕觸摸在 View 的邊界內時,如果手勢和預定的一樣,那會執行回調方法。
PS:一個手勢只能對應一個 View,但是一個 View 可以有多個手勢。建議在真機上測試這些手勢,模擬器操作不太方便,可能導致認為手勢失效的情況。(模擬器測試捏合和旋轉手勢時,按住 option 鍵,再用觸摸板或鼠標操作)
4、舉例說明
功能描述:
附加到兩個圖片視圖 UIImageView 的有『拖動』、『捏合』、『旋轉』、『點按』;
而『輕掃』和『自定義手勢 KMGestureRecognizer』附加在根視圖 UIView 中。
拖動:進行當前圖片視圖位置移動
捏合:進行當前圖片視圖縮放
旋轉:進行當前圖片視圖角度旋轉
點按:雙擊恢復當前圖片視圖的縮放、角度旋轉、不透明度
長按:設置當前圖片視圖的不透明度為0.7
輕掃:左右輕掃設置兩個圖片視圖為居中,同時以垂直居中的特定偏移量定位
自定義手勢:撓癢功能,左右掃動共3次或以上,設置兩個圖片視圖為居中,同時以水平居中的特定偏移量定位
效果如下:
KMGestureRecognizer.h
1 #import <UIKit/UIKit.h> 2 3 typedef NS_ENUM(NSUInteger, Direction) { 4 DirectionUnknown, 5 DirectionLeft, 6 DirectionRight 7 }; 8 9 @interface KMGestureRecognizer : UIGestureRecognizer 10 @property (assign, nonatomic) NSUInteger tickleCount; //撓癢次數 11 @property (assign, nonatomic) CGPoint currentTickleStart; //當前撓癢開始坐標位置 12 @property (assign, nonatomic) Direction lastDirection; //后一次撓癢方向 13 14 @end
KMGestureRecognizer.m
1 #import "KMGestureRecognizer.h" 2 #import <UIKit/UIGestureRecognizerSubclass.h> 3 4 @implementation KMGestureRecognizer 5 #define kMinTickleSpacing 20.0 6 #define kMaxTickleCount 3 7 8 - (void)reset { 9 _tickleCount = 0; 10 _currentTickleStart = CGPointZero; 11 _lastDirection = DirectionUnknown; 12 13 if (self.state == UIGestureRecognizerStatePossible) { 14 self.state = UIGestureRecognizerStateFailed; 15 } 16 } 17 18 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 19 UITouch *touch = [touches anyObject]; 20 _currentTickleStart = [touch locationInView:self.view]; //設置當前撓癢開始坐標位置 21 } 22 23 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 24 //『當前撓癢開始坐標位置』和『移動后坐標位置』進行 X 軸值比較,得到是向左還是向右移動 25 UITouch *touch = [touches anyObject]; 26 CGPoint tickleEnd = [touch locationInView:self.view]; 27 CGFloat tickleSpacing = tickleEnd.x - _currentTickleStart.x; 28 Direction currentDirection = tickleSpacing < 0 ? DirectionLeft : DirectionRight; 29 30 //移動的 X 軸間距值是否符合要求,足夠大 31 if (ABS(tickleSpacing) >= kMinTickleSpacing) { 32 //判斷是否有三次不同方向的動作,如果有則手勢結束,將執行回調方法 33 if (_lastDirection == DirectionUnknown || 34 (_lastDirection == DirectionLeft && currentDirection == DirectionRight) || 35 (_lastDirection == DirectionRight && currentDirection == DirectionLeft)) { 36 _tickleCount++; 37 _currentTickleStart = tickleEnd; 38 _lastDirection = currentDirection; 39 40 if (_tickleCount >= kMaxTickleCount && self.state == UIGestureRecognizerStatePossible) { 41 self.state = UIGestureRecognizerStateEnded; 42 //NSLog(@"自定義手勢成功,將執行回調方法"); 43 } 44 } 45 } 46 } 47 48 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 49 [self reset]; 50 } 51 52 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 53 [self reset]; 54 } 55 56 @end
ViewController.h
ViewController.m
1 #import <UIKit/UIKit.h>
2 #import "KMGestureRecognizer.h"
3
4 @interface ViewController : UIViewController
5 @property (strong, nonatomic) UIImageView *imgV;
6 @property (strong, nonatomic) UIImageView *imgV2;
7 @property (strong, nonatomic) KMGestureRecognizer *customGestureRecognizer;
8
9 @end
1 #import "ViewController.h"
2
3 @interface ViewController ()
4 - (void)handlePan:(UIPanGestureRecognizer *)recognizer;
5 - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;
6 - (void)handleRotation:(UIRotationGestureRecognizer *)recognizer;
7 - (void)handleTap:(UITapGestureRecognizer *)recognizer;
8 - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer;
9 - (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer;
10 - (void)handleCustomGestureRecognizer:(KMGestureRecognizer *)recognizer;
11
12 - (void)bindPan:(UIImageView *)imgVCustom;
13 - (void)bindPinch:(UIImageView *)imgVCustom;
14 - (void)bindRotation:(UIImageView *)imgVCustom;
15 - (void)bindTap:(UIImageView *)imgVCustom;
16 - (void)bindLongPress:(UIImageView *)imgVCustom;
17 - (void)bindSwipe;
18 - (void)bingCustomGestureRecognizer;
19 - (void)layoutUI;
20 @end
21
22 @implementation ViewController
23
24 - (void)viewDidLoad {
25 [super viewDidLoad];
26
27 [self layoutUI];
28 }
29
30 - (void)didReceiveMemoryWarning {
31 [super didReceiveMemoryWarning];
32 // Dispose of any resources that can be recreated.
33 }
34
35 #pragma mark - 處理手勢操作
36 /**
37 * 處理拖動手勢
38 *
39 * @param recognizer 拖動手勢識別器對象實例
40 */
41 - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
42 //視圖前置操作
43 [recognizer.view.superview bringSubviewToFront:recognizer.view];
44
45 CGPoint center = recognizer.view.center;
46 CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
47 CGPoint translation = [recognizer translationInView:self.view];
48 //NSLog(@"%@", NSStringFromCGPoint(translation));
49 recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
50 [recognizer setTranslation:CGPointZero inView:self.view];
51
52 if (recognizer.state == UIGestureRecognizerStateEnded) {
53 //計算速度向量的長度,當他小于200時,滑行會很短
54 CGPoint velocity = [recognizer velocityInView:self.view];
55 CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
56 CGFloat slideMult = magnitude / 200;
57 //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
58
59 //基于速度和速度因素計算一個終點
60 float slideFactor = 0.1 * slideMult;
61 CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
62 center.y + (velocity.y * slideFactor));
63 //限制。踓ornerRadius]和大邊界值[self.view.bounds.size.width - cornerRadius],以免拖動出屏幕界限
64 finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
65 self.view.bounds.size.width - cornerRadius);
66 finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
67 self.view.bounds.size.height - cornerRadius);
68
相關推薦

最新發布
性能測試之測試環境搭建的方法
2020/7/21 15:39:32軟件測試是從什么時候開始被企業所重視的呢?
2020/7/17 9:09:11Android自動化測試框架有哪些?有什么用途?
2020/7/17 9:03:50什么樣的項目適合做自動化?自動化測試人員應具備怎樣的能力?
2020/7/17 8:57:06幾大市面主流性能測試工具測評
2020/7/17 8:52:11RPA機器人能夠快速響應企業需求,是怎么做到的?
2020/7/17 8:48:05Bug可以真正消滅嗎?為什么?
2020/7/17 8:43:03軟件測試基本概念是怎么來的?軟件測試生命周期的形成歷經了什么?
2020/7/16 9:11:10