#import "ViewController.h"@interface ViewController (){ UIView *view1;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor brownColor]; view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; view1.backgroundColor = [UIColor greenColor]; [self.view addSubview:view1];//添加轻点手势 [self addGesture];// 平移手势 [self addPanGesAddSwipeGes];// 长按手势 [self longAction];// 旋转手势 [self rote];// 捏合手势 [self pinchGess]; }#define mark手势轻点- (void)addGesture{ //初始化手势识别器 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init]; //设置点击次数 tap.numberOfTapsRequired = 1; //设置触摸点数 tap.numberOfTouchesRequired = 1; //视图添加手势 [self.view addGestureRecognizer:tap]; //添加target [tap addTarget:self action:@selector(tapAct:)]; //手势的代理设置 tap.delegate = self; }- (void)tapAct:(UIGestureRecognizer*)ges{ NSLog(@"点击了");}//添加旋转手势- (void)rote{ UIRotationGestureRecognizer *rate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(RoteAct:)]; [self.view addGestureRecognizer:rate];}#define mark旋转手势 点击手势的方法改变- (void)RoteAct:(UIRotationGestureRecognizer*)ratation{ if (ratation.state == UIGestureRecognizerStateChanged) { view1.transform = CGAffineTransformMakeRotation(ratation.rotation); }else if(ratation.state == UIGestureRecognizerStateEnded){ [UIView animateWithDuration:.35 animations:^{ //取消形变,恢复原状 view1.transform = CGAffineTransformIdentity; }]; } }#define mark添加平移和清扫手势- (void)addPanGesAddSwipeGes{ //创建平移手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init]; pan.delegate = self; [self.view addGestureRecognizer:pan]; // 设置最大识别数 pan.maximumNumberOfTouches = 1;// pan.minimumNumberOfTouches = 1; //最小识别数 //添加平移手势调用的方法 [pan addTarget:self action:@selector(panAct:)]; //添加清扫手势 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]init]; swipe.delegate = self; [self.view addGestureRecognizer:swipe]; [swipe addTarget:self action:@selector(swipAct:)]; swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;#warning 优先级#define mark 识别器优先级冲突解决方案,优先级低的调用requireGestureRecognizerToFail方法 [pan requireGestureRecognizerToFail:swipe]; }//平移手势调用的方法- (void)panAct:(UIPanGestureRecognizer*)ges{#define mark获取当前位置的偏移量 CGPoint p = [ges translationInView:self.view]; NSLog(@"%@",NSStringFromCGPoint(p));}//清扫手势调用的方法- (void)swipAct:(UISwipeGestureRecognizer*)ges{ NSLog(@"-------%lu",ges.direction);}#define mark长按手势- (void)longAction{ UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]init]; //设置长按时间 longPress.minimumPressDuration = 2; //添加手势 [self.view addGestureRecognizer:longPress]; [longPress addTarget:self action:@selector(action:)];}//长按手势调用的方法- (void)action:(UILongPressGestureRecognizer*)ges{ if (ges.state == UIGestureRecognizerStateBegan) { UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"是否删除" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:@"其它", nil]; [actionSheet showInView:self.view]; }}#pragma mark-捏合手势- (void)pinchGess{ UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; [self.view addGestureRecognizer:pin];}- (void)pinchAction:(UIPinchGestureRecognizer *)pin{ if (pin.state == UIGestureRecognizerStateChanged) { view1.transform = CGAffineTransformMakeScale(pin.scale, pin.scale); }else if(pin.state == UIGestureRecognizerStateEnded){ [UIView animateWithDuration:.35 animations:^{ //取消一切形变 view1.transform = CGAffineTransformIdentity; }]; } }#define mark代理方法 识别器将要开始工作- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ switch (gestureRecognizer.state) { case UIGestureRecognizerStatePossible: NSLog(@"等待"); break; case UIGestureRecognizerStateBegan: NSLog(@"开始"); break; case UIGestureRecognizerStateChanged: NSLog(@"改变"); break; case UIGestureRecognizerStateEnded: NSLog(@"结束"); break; case UIGestureRecognizerStateFailed: NSLog(@"失败"); default: break; } return YES;}//代理方法,识别器将要识别- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ NSLog(@""); return YES;}