1.功能:
Gesture(手势)和touch不同,手势是指手指在屏幕上不同的动作
2.几种重要手势:
(1)UITapGestureRecognizer:(点击)
UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1Action:)];//创建一个点击手势
tap1.numberOfTapsRequired = 1;//设置点击几次触发手势
tap1.numberOfTouchesRequired = 1;//这个设置几根手势去触发手势
//将手势加到某一个视图上,那么为了保证了imageview能接收这个手势,需要让imageview的用户交互打开
[self.imageView addGestureRecognizer:tap1];
//一个视图可以加载多个手势
UITapGestureRecognizer * tap2 =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2Action:)];
tap2.numberOfTapsRequired = 2;
tap2.numberOfTouchesRequired = 1;
//给视图再加一个手势
[self.imageView addGestureRecognizer:tap2];
//上面的两个手势是冲突的,为了避免这种冲突,那么要实现下面这个方法
//下面的方法的意思说:当tap2,执行失败再去执行tap1
[tap1 requireGestureRecognizerToFail:tap2];
//注:以上的tap1Action/tap2Action方法在代码下面都会有实现,即:这个手势要完成的动作
(2)UISwipeGestureRecognizer:(轻扫)
swipeGesture1.direction = UISwipeGestureRecognizerDirectionUp;//轻扫的方向
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
//2个手势同时响应了一个方法
-(void)swipeGestureAction:(UISwipeGestureRecognizer *)swipeGesture
{ [UIView animateWithDuration:0.5 animations:^{
if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp)
center.y -= 20;
else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown)
center.y += 20;
self.view.center = center;}];
}
(3)UIPinchGestureRecognizer:(轻捏)
-(void)pinchGestureRecognizerAction:(UIPinchGestureRecognizer *)pinchGesture
{ [UIView animateWithDuration:pinchGesture.velocity animations:^{
//self.imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGesture.scale, pinchGesture.scale);
//为了防止在缩放时,图片缩放过快,将缩放比例置1
pinchGesture.scale = 1;}];
}
//scale:轻捏时的伸缩比例 velocity:轻捏时的速度
(4)UIRotationGestureRecognizer:(旋转)
-(void)rotationGestureAction:(UIRotationGestureRecognizer *)rotationGesture
{ [UIView animateWithDuration:rotationGesture.velocity animations:^{
//self.imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);
//作用是让旋转的弧度置0,复位
rotationGesture.rotation = 0;}]
}
//rotation:旋转角度 velocity:旋转速度
(5)UIPanGestureRecognizer:(拖拽)
-(void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGesture
{ [UIView animateWithDuration:0.5 animations:^{
//虽然可以使用这个方法来实现拖拽,但并不使用这个
//self.imageView.center = [panGesture locationInView:self.view];
//下面这个方法并不是一个触摸的点
//这个方法返回的是一个偏移量
//如果使用这个方法进行平移,需要使用原始的点加上偏移量进行计算
CGPoint center = self.imageView.center;
CGPoint offset = [panGesture translationInView:self.view];
center.x += offset.x;
center.y += offset.y;
self.imageView.center = center;
//因为偏移量是累加的,所以需要复位
//同样要去做复位操作
[panGesture setTranslation:CGPointMake(0, 0) inView:self.view];}];
}
(6)UILongPressGestureRecognizer:(长按)
//在长按手势发生前,需要先去点满设置的这么多点击次数,如果次数不够,是不能触发长按手势的
longPressGesture.numberOfTapsRequired = 0;
//几根手势触发
longPressGesture.numberOfTouchesRequired = 1;
//设置长按触发的时间
longPressGesture.minimumPressDuration = 1;
//这个属性是在长按时允许移动的范围
longPressGesture.allowableMovement = 20;
3.多手势结合:
注:需要遵守协议,并实现方法
(1)如果想要多手势结合使用,需要遵守一个协议:
<UIGestureRecognizerDelegate>
(2)在遵守该协议的.m文件中实现下面方法,就可以实现多手势结合使用
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{ return YES; }
(3)要在有多手势结合的类中的将自己付给代理:
//创建第一个手势
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
pinch.delegate = self;
[imageView addGestureRecognizer:pinch];
//创建第二个手势
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
rotation.delegate = self;
[imageView addGestureRecognizer:rotation];
(4)实现pinchAction/rotationAction方法即可同时在同一个对象imageView中使用多种手势!