附: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=@"左划横扫";
}
}
}
网友评论