ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
// [self tapGesture];
// [self longPress];
// [self swipeGesture];
// [self panGesture];
[self pinchGesture];
}
#pragma mark -- 点击Tap
- (void)tapGesture{
//1.创建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//2.属性
//点击次数
tap.numberOfTapsRequired =1;
//手指个数
tap.numberOfTouchesRequired =1;
//3.添加手势
[self.view addGestureRecognizer:tap];
/*——————————————————————————————————————————————————————————————————————————————-*/
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
doubleTap.numberOfTapsRequired =2;
[self.view addGestureRecognizer:doubleTap];
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
if (tap.numberOfTapsRequired == 1) {
NSLog(@"single tap");
}else if (tap.numberOfTapsRequired == 2){
NSLog(@"double tap");
}
}
#pragma mark -- 长按 longPress
- (void)longPress{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
//设置长按时间
longPress.minimumPressDuration = 3;
[self.view addGestureRecognizer:longPress];
}
- (void)longAction:(UILongPressGestureRecognizer *)longPress{
NSLog(@"long press");
/**
* UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
*/
NSLog(@"%ld",longPress.state);
}
#pragma mark --轻扫 swipe
- (void)swipeGesture{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//设置轻扫方向
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipe];
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
NSLog(@"右轻扫");
}
#pragma mark --平移 pan
- (void)panGesture{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
//获取平移的坐标点
CGPoint p = [pan translationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(p));
}
#pragma mark --捏合 pinch
- (void)pinchGesture{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
NSLog(@"%f",pinch.scale);
}
#pragma mark --旋转 rotation
- (void)rotationGesture{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
//设置代理
rotation.delegate = self;
[self.view addGestureRecognizer:rotation];
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
NSLog(@"%f",rotation.rotation);
}
#pragma mark --delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
NSLog(@"手势即将开始");
return YES;
}
@end