//七中手势:轻拍(tap) 长按(longPress) 旋转(rotation) 捏合(pinch) 拖拽(pan)
// 轻扫(swipe) 屏幕边缘拖拽(screEdgePan)
//添加图片
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imgView.center = self.view.center;
imgView.image = [UIImage imageNamed:@""];
[self.view addSubview:imgView];
[imgView release];
//打开用户交互
imgView.userInteractionEnabled = YES;
//一 轻拍(tap)
//获取到轻拍手势时,让self调用tapAction:方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//添加手势
[imgView addGestureRecognizer:tap];
//内存管理
[tap release];
//点击次数
tap.numberOfTapsRequired = 2;
//手指个数
tap.numberOfTouchesRequired = 1;
//二 长按(longPress)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction::)];
[imgView addGestureRecognizer:longPress];
[longPress release];
//长按时间
longPress.minimumPressDuration = 0.2;
//三 旋转(rotation)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation];
[rotation release];
//四 捏合(pinch)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAciton:)];
[imgView addGestureRecognizer:pinch];
[pinch release];
//五 轻扫(swipe)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//默认只识别向右
//设置方向时 最多只能设置 水平(左/右)或者垂直(上/下)
swipe.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight |UISwipeGestureRecognizerDirectionDown |UISwipeGestureRecognizerDirectionUp;
[imgView addGestureRecognizer:swipe];
[swipe release];
//六 pan(拖拽)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[imgView addGestureRecognizer:pan];
[pan release];
//七 屏幕边缘拖拽(sreenEdgePan)
UIScreenEdgePanGestureRecognizer *sep = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(sepAction:)];
//需要设置拖拽的边缘
sep.edges = UIRectEdgeLeft;
//一般这个手势添加在VC的view上
[self.view addGestureRecognizer:sep];
[sep release];
网友评论