UITapGestureRecognizer *tap= [[UITapGestureRecognizeralloc] init];
// 連續敲擊2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲擊
tap.numberOfTouchesRequired = 2;
[self.iconViewaddGestureRecognizer:tap];
[tap addTarget:selfaction:@selector(tapIconView:)];
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
//沒有觸摸事件發生,所有手勢識別的默認狀態
UIGestureRecognizerStatePossible
//一個手勢已經開始但尚未改變或者完成時
UIGestureRecognizerStateBegan,
// 手勢狀態改變
UIGestureRecognizerStateChanged,
// 手勢完成
UIGestureRecognizerStateEnded,
// 手勢取消,恢復至Possible狀態
UIGestureRecognizerStateCancelled,
//手勢失敗,恢復至Possible狀態
UIGestureRecognizerStateFailed,
// 識別到手勢識別
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
- (void)viewDidLoad {
[super viewDidLoad];
// 1、創建一個敲擊手勢
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
// 2、設置手勢對象的屬性
// 點擊次數
tapGesture.numberOfTapsRequired = 1;
// 手指的數量
// tapGesture.numberOfTouchesRequired = 2;
// 設置代理
tapGesture.delegate = self;
// 3、把手勢添加到view上
[self.view addGestureRecognizer:tapGesture];
// 4、設置手勢的監聽方法
[tapGesture addTarget:self action:@selector(tapView:)];
}
- (void)tapView:(UITapGestureRecognizer *)tapGest{
NSLog(@"敲擊手勢的監聽方法");
}
長按手勢:
- (void)viewDidLoad {
[super viewDidLoad];
// 1、創建一個 “長按手勢” 對象
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] init];
// 2、設置屬性
// 長按最少持續多長時間
longGesture.minimumPressDuration = 2;
// 長按時,距離 “觸摸點” 可移動的距離
longGesture.allowableMovement = 30;
// 3、把手勢添加到view上
[self.imageView addGestureRecognizer:longGesture];
// 4、設置手勢的監聽方法
[longGesture addTarget:self action:@selector(longPressView:)];
}
- (void)longPressView:(UILongPressGestureRecognizer *)longPressGesture{
// 怎麼判斷長按開始和結束
// 通過手勢的狀態來判斷
if (longPressGesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按手勢開始");
}else{
NSLog(@"長按手勢結束");
}
}
輕掃手勢:
- (void)viewDidLoad {
[super viewDidLoad];
// 1、創建一個 “輕掃手勢” 對象
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
// 2、設置屬性
//UISwipeGestureRecognizerDirectionRight 向右輕掃
//UISwipeGestureRecognizerDirectionLeft 向左輕掃
//UISwipeGestureRecognizerDirectionUp 向上
//UISwipeGestureRecognizerDirectionDown 向下
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
// 3、把手勢添加到view上
[self.imageView addGestureRecognizer:swipeGesture];
}
- (void)swipeView:(UIGestureRecognizer *)swipeGest{
// 怎麼判斷 "長按" 開始和結束
NSLog(@"%s 手勢狀態 %ld",__func__, swipeGest.state);
}
捏合手勢:
- (void)viewDidLoad {
[super viewDidLoad];
// 1、創建一個 “捏合手勢” 對象
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinView:)];
// 2、把手勢添加到 view上
[self.imageView addGestureRecognizer:pinchGest];
}
- (void)pinView:(UIPinchGestureRecognizer *)pinGest{
// 縮放的比例是一個 “累加” 的過程
#warning 放大圖片後,再次縮放的時候,馬上回到原先的大小
// self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinGest.scale, pinGest.scale);
// 讓比例還原 ,不累加
// 解決辦法
pinGest.scale = 1;
}
旋轉手勢:
- (void)viewDidLoad {
[super viewDidLoad];
// 1、添加一個 “旋轉手勢” 對像
UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
// 2、把手勢添加到 view上
[self.imageView addGestureRecognizer:rotationGest];
}
- (void)rotationView:(UIRotationGestureRecognizer *)rotationGesture{
// 旋轉角度
// 旋轉角度也是一個累加過程
// 設置圖片的旋轉
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);
// 清除 “旋轉角度” 的累加
rotationGesture.rotation = 0;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 默認情況下,控件只能監聽到一種手勢
// 如果要監聽到多個手勢,設置一個代理的方法,告知它允許 “多個手勢” 並存
// 給圖片添加一個 “旋轉手勢” 對象
// 1、創建一個 “旋轉手勢” 對象
UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
// 設置代理
rotationGest.delegate = self;
// 2、把手勢添加到 view上
[self.view addGestureRecognizer:rotationGest];
// 3、給圖片添加 “捏合手勢”
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[self.imageView addGestureRecognizer:pinchGest];
}
- (void)rotationView:(UIRotationGestureRecognizer *)rotationGesture{
// 旋轉角度
// 旋轉角度也是一個累加過程
// 設置圖片的旋轉
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);
// 清除 “旋轉角度” 的累加
rotationGesture.rotation = 0;
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinGest{
// 縮放的比例是一個 “累加” 的過程
#warning 放大圖片後,再次縮放的時候,馬上回到原先的大小
// self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinGest.scale, pinGest.scale);
// 讓比例還原 ,不累加
// 解決辦法
pinGest.scale = 1;
}
// simultaneous 同時發生
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
拖拽手勢:
- (void)viewDidLoad {
[super viewDidLoad];
// 給圖片添加 “拖拽手勢”
// 1、創建一個 “拖拽手勢” 對象
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
// 2、添加到view
[self.imageView addGestureRecognizer:panGest];
}
- (void)panView:(UIPanGestureRecognizer *)panGest{
// panGest.view 觸摸的view
// 拖拽的距離 (距離是一個累加的過程)
CGPoint trans = [panGest translationInView:panGest.view];
// 設置圖片移動
CGPoint center = self.imageView.center;
center.x += trans.x;
center.y += trans.y;
self.imageView.center = center;
// 清除累加的距離
[panGest setTranslation:CGPointZero inView:panGest.view];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~