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

附: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=@"左划横扫";

}
    
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容