IOS 自定义手势方向

//自定义一个类继承于 UIPanGestureRecognizer

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

   UITouch *touch=[touches anyObject];

   startPoint=[touch locationInView:self.view];

    _curPoint = startPoint;

}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

   UITouch *touch=[touches anyObject];

   endPoint=[touch locationInView:self.view];

    self.curPoint =endPoint;

   CGFloat moveAmtX = endPoint.x - startPoint.x;

   CGFloat moveAmtY = endPoint.y - startPoint.y;

   self.curMoveAmtX = moveAmtX;

   self.curMoveAmtY = moveAmtY;

    

   CGFloat moveAmtYAbs = fabs(moveAmtY);

   //斜边

   CGFloat moveHypotenuse = sqrt(moveAmtX*moveAmtX + moveAmtY*moveAmtY);

    //  1.57 0.785

   CGFloat rate = moveAmtYAbs*1.0/moveHypotenuse;

    //通过调这个rate值来定义你想触发的手势方向的作用范围.  

   if(asin(rate) >=1.0){

         [selfsetState:UIGestureRecognizerStateChanged];

    }else{

       return ;

    }

}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    startPoint=CGPointZero;

    _curPoint = startPoint;

    [selfsetState:UIGestureRecognizerStateEnded];

   return ;

}



你可能感兴趣的:(IOS 自定义手势方向)