UIGestureRecognizer手勢(shì)識(shí)別器
手勢(shì):有規(guī)律的觸摸
UIGestureRecognizer抽象類
七種手勢(shì):輕拍(tap)長(zhǎng)按(longPress)旋轉(zhuǎn)(rotation)捏合(pinch)拖拽(pan)輕掃(swipe)屏幕邊緣拖拽(screenEdgePan)
輕拍tap
創(chuàng)建對(duì)象
獲取到輕拍手勢(shì)時(shí)讓self調(diào)用tapAction:方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapAction:)];
添加手勢(shì)
[imgView addGestureRecognizer:tap];
內(nèi)存管理
[tap release];
點(diǎn)擊次數(shù)
tap.numberOfTapsRequired =2;
手指?jìng)€(gè)數(shù)
tap.numberOfTouchesRequired =2;
#pragma mark -輕拍tap
- (void)tapAction:(UITapGestureRecognizer*)tap
{
NSLog(@"輕拍");
}
長(zhǎng)按longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(longPressAction:)];
[imgView addGestureRecognizer:longPress];
[longPress release];
長(zhǎng)按時(shí)間
longPress.minimumPressDuration =1;
#pragma mark -長(zhǎng)按longPress
- (void)longPressAction:(UILongPressGestureRecognizer*)longPress
{
if(longPress.state==UIGestureRecognizerStateBegan) {
NSLog(@"長(zhǎng)按");
}
}
旋轉(zhuǎn)rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation];
[rotation release];
#pragma mark -旋轉(zhuǎn)rotation
- (void)rotationAction:(UIRotationGestureRecognizer*)rotation
{
*UIView transform屬性專門用來(lái)進(jìn)行形變(位置position/旋轉(zhuǎn)rotation/縮放scale)設(shè)置
UIImageView*imgView = (UIImageView*)rotation.view;
imgView.transform=CGAffineTransformMakeRotation(rotation.rotation);
NSLog(@"旋轉(zhuǎn)");
}
捏合pinch
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinch];
[pinch release];
#pragma mark -捏合pinch
- (void)pinchAction:(UIPinchGestureRecognizer*)pinch
{
UIImageView*imgView = (UIImageView*)pinch.view;
imgView.transform=CGAffineTransformMakeScale(pinch.scale, pinch.scale);
//縮放scale(比例)(下面兩行代碼效果等同于上一行)
//??? imgView.transform = CGAffineTransformScale(imgView.transform, pinch.scale, pinch.scale);
//??? pinch.scale = 1;
NSLog(@"捏合");
}
拖拽pan
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];
[imgViewaddGestureRecognizer:pan];
[panrelease];
#pragma mark -拖拽pan
- (void)panAction:(UIPanGestureRecognizer*)pan
{
NSLog(@"拖拽");
UIImageView*imgView = (UIImageView*)pan.view;
CGPointp = [pantranslationInView:imgView];
imgView.transform=CGAffineTransformMakeTranslation(p.x, p.y);
//設(shè)置跟隨屬性(下面兩行代碼效果等同于上一行)
//??? imgView.transform = CGAffineTransformTranslate(imgView.transform, p.x, p.y);
//??? [pan setTranslation:CGPointZero inView:imgView];
}
輕掃swipe
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeAction:)];
默認(rèn)只識(shí)別向右
設(shè)置方向時(shí)最多只能設(shè)置水平(左/右)或者垂直(上/下)
swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[imgView addGestureRecognizer:swipe];
[swipe release];
#pragma mark -輕掃swipe
- (void)swipeAction:(UISwipeGestureRecognizer*)swipe
{
if(swipe.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左");
}
NSLog(@"輕掃");
}
屏幕邊緣拖拽screenEdgePan
UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];
sep.edges=UIRectEdgeLeft;
[self.viewaddGestureRecognizer:sep];
[seprelease];
#pragma mark -屏幕邊緣拖拽screenEdgePan
- (void)sepAction:(UIScreenEdgePanGestureRecognizer*)sep
{
NSLog(@"屏幕邊緣");
}