//----------手势类 继承自UIGestureRecognizer
//需要引入的头文件
#import
#import"SwipGestureRecognizer.h"
@implementationSwipGestureRecognizer
//记录起始点的Y坐标
CGFloatbaseY;
CGFloatprevX;
NSIntegercount;
NSUIntegerprevDir;//定义手指一动的方向,1代表向右2代表向左
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
[supertouchesBegan: toucheswithEvent:event];
//获取多个触碰点中任意一个触碰点
UITouch* touch=[touchesanyObject];
//获取碰触点在self.view中的坐标
CGPointpoint=[touchlocationInView:self.view];
baseY=point.y;
prevX=point.x;
count=0;
prevDir=0;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event{
[supertouchesMoved:toucheswithEvent:event];
UITouch* touch=[touchesanyObject];
CGPointcurrPoint=[touchlocationInView:self.view];
//如果垂直方向上一动距离过大,则取消手势
if(fabs(currPoint.y-baseY)>10) {
self.state=UIGestureRecognizerStateCancelled;
return;
//fabs
// C语言数学函数:fabs
//原型:在TC中原型是extern float fabs(float x);,在VC6.0中原型是double fabs(double x );。
//用法:#include
//功能:求浮点数x的绝对值
//说明:计算|x|,当x不为负时返回x,否则返回-x
}
NSUIntegercurrDir=currPoint.x>prevX?1:2;
//刚开始还没有初始化方向时
if(prevDir==0) {
prevDir=currDir;
}
//如果前一次的移动方向与当前移动方向不同,则将“摆动”次数加1
if(prevDir!=currDir) {
//将“摆动”次数加1
count++;
//使用prevDir纪录为当前的摆动方向
prevDir=currDir;
}
//如果改变方向的次数超过了该手势处理器要求的次数,判断手势成功
if(count>=self.swipCount) {
self.state=UIGestureRecognizerStateEnded;
}
}
@end
//---------使用 和系统手势使用方法相同
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor=[UIColorlightGrayColor];
self.imageVi=[[UIImageViewalloc]initWithFrame:CGRectMake(100,100,200,200)];
self.imageVi.layer.borderWidth=2;
self.imageVi.layer.cornerRadius=6;
self.imageVi.userInteractionEnabled=YES;
self.imageVi.multipleTouchEnabled=YES;
[self.viewaddSubview:self.imageVi];
SwipGestureRecognizer* swipGesture=[[SwipGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwing:)];
swipGesture.swipCount=2;
[self.imageViaddGestureRecognizer:swipGesture];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)handleSwing:(UISwipeGestureRecognizer*)gesture{
NSUIntegertouchNum=gesture.numberOfTouches;
UIAlertView* alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"123"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"确定",nil];
[alertshow];
}