再我们写一个APP的时候,经常需要给APP添加手势 如:轻拍.横扫.长按等大概7种手势吧.(具体我也忘了 写着看吧 数数有几种)

 手势是一个名为 UIGestureRecognizer 的类 这是一个抽象类(抽象类本身不实现什么功能,具体能力又其子类去实现 ) 所以我们需要的是UIGestureRecognizer的子类添加手势

添加手势一般就是三个步骤 1. 创建手势 2. 添加手势到View  3.释放手势 (ARC 不需要释放)

1. 轻拍手势 UITapGestureRecognizer

//创建一个轻拍手势  初始化的时候可以添加一个方法 并且选执行对象
 //   UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>
//把时候添加到一个View上面
    self.view addGestureRecognizer:<#(nonnull UIGestureRecognizer *)#>
//执行对象为self 方法名为 tapAcion 轻拍手势添加到self.view上
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAcion)];
     [self.view addGestureRecognizer:tap];
 //实现轻拍方法
 - (void)tapAcion{

     NSLog(@"你轻轻的拍了我一下");

 }

iOS 添加手势 ios设置手势_抽象类


2.长按手势

//创建一个长按手势 并添加方法
    UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
     //设置长按 时间 属性   //长按属性 需要程序猿设定多久才能触发这个手势 
//我们设定两秒吧
   longpress.minimumPressDuration = 2.0; //这是按住的最短持续时间 以秒为单位
     [self.view addGestureRecognizer:longpress];//实现 长按
 - (void)longPressAction:(UILongPressGestureRecognizer *)longpress{
     //判断一下手势状态 只需要触发一次
     if (longpress.state == UIGestureRecognizerStateBegan) {  //这里用到了手势状态的状态 可以点进去看一下 这个一个结构体 
         NSLog(@"按住两秒才行啊");
     }
 }


iOS 添加手势 ios设置手势_iOS 添加手势_02


//下面方法的实现 都是打印出来一句话 就不一一展示了 
3.    边缘扫 UIScreenEdgePanGestureRecognizer
    //边缘扫
     UIScreenEdgePanGestureRecognizer *edgepan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePanAction:)];
     //设置一下从哪一个边缘扫    edgepan.edges = UIRectEdgeRight; //设置边缘扫的方向 
   //点进去edges 这是一个结构体 里面包含了 边缘扫可以扫的方向
    UIRectEdgeNone   = 0, 
     UIRectEdgeTop    = 1 << 0,
     UIRectEdgeLeft   = 1 << 1,
     UIRectEdgeBottom = 1 << 2,
     UIRectEdgeRight  = 1 << 3,
     UIRectEdgeAll    = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight

4.    轻扫 UISwipeGestureRecognizer
     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
 //    设置一下 左右扫的方向
     swipe.direction = UISwipeGestureRecognizerDirectionLeft; //轻扫 需要设置轻扫的一个方向 点进去可以看到 就是向上.下.左.右 方向
     [self.view addGestureRecognizer:swipe]; 

5.  旋转手势  UIRotationGestureRecognizer
     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
     [self.view addGestureRecognizer:rotation];6    平移手势
     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
     [self.view addGestureRecognizer:pan];7    捏合手势
     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
     [self.view addGestureRecognizer:pinch];
//以上几个手势是不需要代理 可以直接添加到View上的  
// 手势 还是有一个代理的 可以设置手势的属性  UIGestureRecognizerDelegate
// 看这个方法 一般手势不可以两个同时被系统出发的 ( 同时不是先后 )  这个方法 返回YES 就是允许同时识别两个
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//其他还有几个方法 用到的时候 可以看看