手勢識別在 iOS 中非常重要,他極大地提高了移動設備的使用便捷性。
一、介紹
1、iOS 系統在 3.2 以後,他提供了一些常用的手勢(UIGestureRecognizer 的子類),開發者可以直接使用他們進行手勢操作。
- UIPanGestureRecognizer(拖動)
- UIPinchGestureRecognizer(捏合)
- UIRotationGestureRecognizer(旋轉)
- UITapGestureRecognizer(點按)
- UILongPressGestureRecognizer(長按)
- UISwipeGestureRecognizer(輕掃)
在六種手勢識別中,只有一種手勢是離散型手勢,他就是 UITapGestureRecognizer。
離散型手勢的特點就是:一旦識別就無法取消,而且只會調用一次手勢操作事件(初始化手勢時指定的回調方法)。
換句話說其他五種手勢是連續型手勢,而連續型手勢的特點就是:會多次調用手勢操作事件,而且在連續手勢識別後可以取消手勢。
2、手勢的枚舉:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // 尚未識別是何種手勢操作(但可能已經觸發了觸摸事件),默認狀態
UIGestureRecognizerStateBegan, // 手勢已經開始,此時已經被識別,但是這個過程中可能發生變化,手勢操作尚未完成
UIGestureRecognizerStateChanged, // 手勢狀態發生轉變
UIGestureRecognizerStateEnded, // 手勢識別操作完成(此時已經鬆開手指)
UIGestureRecognizerStateCancelled, // 手勢被取消,恢復到默認狀態
UIGestureRecognizerStateFailed, // 手勢識別失敗,恢復到默認狀態
UIGestureRecognizerStateRecognized= UIGestureRecognizerStateEnded // 手勢識別完成,同UIGestureRecognizerStateEnded
3、使用手勢的步驟
使用手勢很簡單,分爲三步:
(1)創建手勢識別器對象實例。創建時,指定一個回調方法,當手勢開始,改變、或結束時,執行回調方法。
(2)設置手勢識別器對象實例的相關屬性(可選部分)
(3)添加到需要識別的 View 中。每個手勢只對應一個 View,當屏幕觸摸在 View 的邊界內時,如果手勢和預定的一樣,那就會執行回調方法。
二、代碼示例
#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()
{
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 100, 200)];
[self.view addSubview:imageView];
// 點擊手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tick:)];
// 手指點擊屏幕的次數
tap.numberOfTapsRequired = 1;
// 幾個手指點擊
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];
// 長按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(loagPress:)];
// 最少按多少秒
longPress.minimumPressDuration = 3;
[self.view addGestureRecognizer:longPress];
// 輕掃
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
// 輕掃的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe];
// 拖動
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[pan requireGestureRecognizerToFail:swipe];
[self.view addGestureRecognizer:pan];
// 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch];
// 旋轉
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
rotation.rotation = 1;
[self.view addGestureRecognizer:rotation];
}
- (void)tick:(UITapGestureRecognizer *)tap
{
// 6、再出始化 這個對象的 地方 掛上代理
// NextViewController *next = [[NextViewController alloc]init];
// next.delegate = self;
//
// [self presentViewController:next animated:YES completion:nil];
imageView.transform = CGAffineTransformIdentity;
// 獲取點擊屏幕的位置
NSLog(@"tap%f %f",[tap locationInView:self.view].x,[tap locationInView:self.view].y);
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
imageView.center = [tap locationInView:self.view];
[UIView animateWithDuration:0.5 animations:^{
imageView.alpha = 0.01;
}];
}
- (void)mmmmmm
{
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
}
- (void)loagPress:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"longPress%f %f",[longPress locationInView:self.view].x,[longPress locationInView:self.view].y);
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"swipe%f %f",[swipe locationInView:self.view].x,[swipe locationInView:self.view].y);
self.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView animateWithDuration:0.8 animations:^{
self.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取拖動的位置
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
imageView.center = [pan locationInView:self.view];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
// 捏合的變化規模
pinch.scale = 1;
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
// 使旋轉手勢上的視圖旋轉變化
imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
}