為了完成手勢識別,必須借助於手勢識別器—-UIGestureRecognizer
利用UIGestureRecognizer,能輕松識別用戶在某個view上面做的一些常見手勢
UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢
UITapGestureRecognizer //(敲擊)
UIPinchGestureRecognizer //(捏合,用於縮放)
UIPanGestureRecognizer //(拖拽)
UISwipeGestureRecognizer //(輕掃)
UIRotationGestureRecognizer //(旋轉)
UILongPressGestureRecognizer //(長按)
每一個手勢識別器的用法都差不多,比如UITapGestureRecognizer的使用步驟如下
創建手勢識別器對象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
設置手勢識別器對象的具體屬性
// 連續敲擊2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲擊
tap.numberOfTouchesRequired = 2;
添加手勢識別器到對應的view上
[self.iconView addGestureRecognizer:tap];
監聽手勢的觸發
[tap addTarget:self action:@selector(tapIconView:)];
@interface GRViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end
@implementation GRViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self testTap2];
}
- (void)testTap2
{
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
tap.delegate = self;
[self.iconView addGestureRecognizer:tap];
}
#pragma mark - 代理方法
/**
* 當點擊view的時候,會先調用這個方法
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint pos = [touch locationInView:touch.view];
if (pos.x <= self.iconView.frame.size.width * 0.5) {
return YES;
}
return NO;
}
- (void)testTap
{
// 1.創建手勢識別器對象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
// 連續敲擊2次,手勢才能識別成功
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 2;
// 2.添加手勢識別器對象到對應的view
[self.iconView addGestureRecognizer:tap];
// 3.添加監聽方法(識別到了對應的手勢,就會調用監聽方法)
[tap addTarget:self action:@selector(tapView)];
}
- (void)tapView
{
NSLog(@"-----我敲擊了屏幕!!");
}
@end
@interface GRViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation GRViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.redView addGestureRecognizer:swipe];
}
- (void)swipeView
{
NSLog(@"swipeView");
}
- (void)testLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
[longPress addTarget:self action:@selector(longPressView)];
// 至少長按2秒
longPress.minimumPressDuration = 2;
// 在觸發手勢之前,50px范圍內長按有效
longPress.allowableMovement = 50;
[self.redView addGestureRecognizer:longPress];
}
- (void)longPressView
{
NSLog(@"長按了紅色的view");
}
@end
@interface GRViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end
@implementation GRViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self testPinchAndRotate];
}
#pragma mark - 手勢識別器的代理方法
/**
* 是否允許多個手勢識別器同時有效
* Simultaneously : 同時地
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - 縮放 + 旋轉
- (void)testPinchAndRotate
{
[self testPinch];
[self testRotate];
}
#pragma mark - 縮放手勢(捏合手勢)
- (void)testPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
pinch.delegate = self;
[self.iconView addGestureRecognizer:pinch];
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1; // 這個真的很重要!!!!!
}
#pragma mark - 旋轉手勢
- (void)testRotate
{
UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
recognizer.delegate = self;
[self.iconView addGestureRecognizer:recognizer];
}
- (void)rotateView:(UIRotationGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0; // 這個很重要!!!!!
}
@end
@interface GRViewController ()
@property (weak, nonatomic) IBOutlet UIView *purpleView;
@end
@implementation GRViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[self.purpleView addGestureRecognizer:pan];
}
- (void)panView:(UIPanGestureRecognizer *)pan
{
switch (pan.state) {
case UIGestureRecognizerStateBegan: // 開始觸發手勢
break;
case UIGestureRecognizerStateEnded: // 手勢結束
break;
default:
break;
}
// 1.在view上面挪動的距離
CGPoint translation = [pan translationInView:pan.view];
CGPoint center = pan.view.center;
center.x += translation.x;
center.y += translation.y;
pan.view.center = center;
// 2.清空移動的距離
[pan setTranslation:CGPointZero inView:pan.view];
}
@end
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
// 沒有觸摸事件發生,所有手勢識別的默認狀態
UIGestureRecognizerStatePossible,
// 一個手勢已經開始但尚未改變或者完成時
UIGestureRecognizerStateBegan,
// 手勢狀態改變
UIGestureRecognizerStateChanged,
// 手勢完成
UIGestureRecognizerStateEnded,
// 手勢取消,恢復至Possible狀態
UIGestureRecognizerStateCancelled,
// 手勢失敗,恢復至Possible狀態
UIGestureRecognizerStateFailed,
// 識別到手勢識別
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};