美文网首页
ios 学习小结之判断手势移动方向距离

ios 学习小结之判断手势移动方向距离

作者: zeroskylian | 来源:发表于2017-04-21 11:30 被阅读160次

    附:http://blog.csdn.net/volcan1987/article/details/6677370

    先宏定义

    
    //判定方向距离
    
    #define touchDistance 100
    
    //偏移
    
    #define touchPy 10
    
    

    //属性

    
    @property (assign) CGPoint beginPoint;
    
    @property (assign) CGPoint movePoint;
    
    @property (strong,nonatomic)UITextField *tectFie;
    
    
    
    //在viewDidLoad中定义一个显示方向的textField
    
    _tectFie = [[UITextField alloc]initWithFrame:CGRectMake(20, 50, 100, 50)];
    
    _tectFie.backgroundColor = [UIColor whiteColor];
    
    _tectFie.delegate = self;
    
    [self.view addSubview:_tectFie];
    

    在-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event中获取beginPoint的值

    UITouch *touch=[touches anyObject];
    
    self.beginPoint=[touch locationInView:self.view];
    

    在-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)even中 获取movePoint计算偏移

    NSInteger touchCount=[touches count];
    
    self.lab.text=[NSString stringWithFormat:@"%ld",(long)touchCount];
    
    UITouch *touch=[touches anyObject];
    
    self.movePoint=[touch locationInView:self.view];
    
    // 计算偏移值,取绝对值
    
    int deltaX=fabs(self.movePoint.x-self.beginPoint.x);
    
     int deltaY=fabs(self.movePoint.y-self.beginPoint.y);
    
       if (deltaX > touchDistance && deltaY<=touchPy)    {
    
          self.tectFie.text=@"横扫";
    
        }
    
      if (deltaY > touchDistance && deltaX<=touchPy)
    
     {
    
      self.tectFie.text=@"竖扫";
    
      }
    
    int changeX = self.movePoint.x-self.beginPoint.x;
    
    int deltaX=fabs(self.movePoint.x-self.beginPoint.x);
    
    int deltaY=fabs(self.movePoint.y-self.beginPoint.y);
    
    if (changeX > 0) {
    
    NSLog(@"右划");
    
    if (deltaX > touchDistance && deltaY<=touchPy)
    
    {
    
    self.tectFie.text=@"右划横扫";}
    
    }else
    
    {
    
    NSLog(@"左划");
    
    if (deltaX > touchDistance && deltaY<=touchPy)
    
    {
    
    self.tectFie.text=@"左划横扫";
    
    }
        
    }
    }
    

    相关文章

      网友评论

          本文标题:ios 学习小结之判断手势移动方向距离

          本文链接:https://www.haomeiwen.com/subject/uohdzttx.html