UIGestureRecognizer手势识别器
手势:有规律的触摸
UIGestureRecognizer抽象类:七种手势:轻拍(tap)长按(longPress)旋转(rotation)捏合(pinch)拖拽(pan)轻扫(swipe)屏幕边缘拖拽(screenEdgePan)
首先:添加图片 以便于观察手势
添加图片的步骤
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,200,200)];
imgV.center = self.view.center;
imgV.image = [UIImage imageNamed:@"blank"];//blank是图片的名称
self.view addSubview:imgV];
[imgV release];
//打开用户交互
imgV.userInteractionEnable = YES;
轻拍tap
创建对象
获取到轻拍手势 让 self调用tapAction:方法
UITapGestureREcognizer *tap = [[UITapGestureREcognizer alloc] initWithTarget:self action:@selector(tapAction:)];
添加手势
imgV addGestureRecognizer:tap];
内存管理
tap release];
点击次数
tap.numberOfTapsRequired = 2;
手指个数
tap.numberOfTouchsRequired = 2;
长按longPress
UILongPressGestureRecognizer *longPrss = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[imgV addGestureREcognizer:longPress];
[longPress release];
长按时间
longPress.minimumPressDuration = 1;
旋转rotation
UIRatationGestureRecognizer *rotation = [UIRatationGestureRecognizer alloc]initWithTarget:self action@selector(rotationAction:)];
[imgV addGestureREcognizer:rotation];
[rotation release];
捏合pinch
UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgV addGestureREcognizer:pinch];
[pinch release];
拖拽pan
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];
[imgViewaddGestureRecognizer:pan];
[panrelease];
轻扫swipe
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeAction:)];
默认只识别向右
设置方向时最多只能设置水平(左/右)或者垂直(上/下)
swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;//(按位或)
[imgView addGestureRecognizer:swipe];
[swipe release];
屏幕边缘拖拽screenEdgePan
UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];
需要设置拖拽的边缘
sep.edges=UIRectEdgeLeft;
一般这个手势添加在VC的view上
[self.viewaddGestureRecognizer:sep];
[seprelease];
网友评论