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 -- 點(diǎn)擊Tap
- (void)tapGesture{
//1.創(chuàng)建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//2.屬性
//點(diǎn)擊次數(shù)
tap.numberOfTapsRequired =1;
//手指?jìng)€(gè)數(shù)
tap.numberOfTouchesRequired =1;
//3.添加手勢(shì)
[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:)];
//設(shè)置長按時(shí)間
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:)];
//設(shè)置輕掃方向
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{
//獲取平移的坐標(biāo)點(diǎn)
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 --旋轉(zhuǎn) rotation
- (void)rotationGesture{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
//設(shè)置代理
rotation.delegate = self;
[self.view addGestureRecognizer:rotation];
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
NSLog(@"%f",rotation.rotation);
}
#pragma mark --delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
NSLog(@"手勢(shì)即將開始");
return YES;
}
@end