IOS 自定义手势方向

来源:互联网 发布:proteus仿真软件下载 编辑:程序博客网 时间:2024/05/21 12:34

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

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

   UITouch *touch=[touchesanyObject];

   startPoint=[touchlocationInView:self.view];

    _curPoint = startPoint;

}


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

   UITouch *touch=[touchesanyObject];

   endPoint=[touchlocationInView: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 ;

}



0 0
原创粉丝点击