UIGestureRecognizer手勢識別器
手勢:有規(guī)律的觸摸
UIGestureRecognizer抽象類:七種手勢:輕拍(tap)長按(longPress)旋轉(zhuǎn)(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
創(chuàng)建對象
獲取到輕拍手勢 讓 self調(diào)用tapAction:方法
UITapGestureREcognizer *tap = [[UITapGestureREcognizer alloc] initWithTarget:self action:@selector(tapAction:)];
添加手勢
imgV addGestureRecognizer:tap];
內(nèi)存管理
tap release];
點擊次數(shù)
tap.numberOfTapsRequired = 2;
手指個數(shù)
tap.numberOfTouchsRequired = 2;
長按longPress
UILongPressGestureRecognizer *longPrss = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[imgV addGestureREcognizer:longPress];
[longPress release];
長按時間
longPress.minimumPressDuration = 1;
旋轉(zhuǎn)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:)];
默認(rèn)只識別向右
設(shè)置方向時最多只能設(shè)置水平(左/右)或者垂直(上/下)
swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;//(按位或)
[imgView addGestureRecognizer:swipe];
[swipe release];
屏幕邊緣拖拽screenEdgePan
UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];
需要設(shè)置拖拽的邊緣
sep.edges=UIRectEdgeLeft;
一般這個手勢添加在VC的view上
[self.viewaddGestureRecognizer:sep];
[seprelease];