今天项目遇到了各种手势,索性把所有手势全都收集了一遍,也不是全部吧,但是常用的都在这里了 ,手势效果除了建档的点击手势和长安手势之外,其他手势都需要注意会有累加效果,累加效果每次如果不给于重置,手势将会进行叠加,叠加的效果就是,根本停不下来!!!!!!!!
遵守UIGestureRecognizerDelegate 协议
//点击的手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
//设定点击次序,识别手势的时候使用
tapGesture.numberOfTapsRequired = 2;
//[view addGestureRecognizer:tapGesture];
//添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)];
//设置按压最短持续时间,默认0.5s
longPress.minimumPressDuration = 1;
[view addGestureRecognizer:longPress];
//移动的手势,滑动的手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
[view addGestureRecognizer:panGesture];
//捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
pinchGesture.delegate = self;
[view addGestureRecognizer:pinchGesture];
//旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)];
rotationGesture.delegate = self;
[view addGestureRecognizer:rotationGesture];
#pragma mark -
#pragma mark Handle Gesture Recoginzer
- (void)handleRotationGesture:(UIRotationGestureRecognizer*)gesture{
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
UIView *view = gesture.view;
[self.view bringSubviewToFront:view];
//旋转的弧度
CGFloat rotation = gesture.rotation;
view.transform = CGAffineTransformRotate(view.transform, rotation);
//注意有累加
gesture.rotation = 0.0;
}
}
- (void)handlePinchGesture:(UIPinchGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
UIView *view = gesture.view;
[self.view bringSubviewToFront:view];
//得到缩放的比例,然后设置transform的缩放参数
//gesture.scale 捏合时候的缩放比例
CGFloat scale = gesture.scale;
//设置view.transform scale 代表缩放
//第一个参数,是基准值,在此基础上进行缩放
//第二个参数,x 坐标方向的缩放比例
//第三个参数,y 坐标方向的缩放比例
view.transform = CGAffineTransformScale(view.transform, scale, scale);
//由于手势效果会累积,所以需要给他复位
gesture.scale = 1.0;
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
//手势识别成功,并且手势持续进行中
UIView *view = gesture.view;
[self.view bringSubviewToFront:view];
//手势在self.view上移动的多少
CGPoint offset = [gesture translationInView:self.view];
CGPoint newCenter = CGPointMake(view.center.x+offset.x, view.center.y+offset.y);
view.center = newCenter;
//手势的效果会累加,translation一直累加
//重新初始化,去除原来的累加效果
[gesture setTranslation:CGPointZero inView:self.view];
}
}
//点击手势处理
- (void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
//gesture.view; 代表手势发生在哪一个view上
NSLog(@"点击了2次,手势识别成功");
}
//手势识别的过程,首先接受到touchbegin的事件,开始启动手势识别,手势的状态
//UIGestureRecognizerStatePossible,然后继续接收并分析触摸事件,如果手势识别成功,手势的状态变为UIGestureRecognizerStateBegan
//如果手势继续进行,手势的状态变为UIGestureRecognizerStateChanged
//如果手势结束,状态变为UIGestureRecognizerStateEnded
//如果手势识别失败 UIGestureRecognizerStateFailed
- (void)handleLongPressGesture:(UILongPressGestureRecognizer*)gestrue
{
if (gestrue.state == UIGestureRecognizerStateBegan || gestrue.state == UIGestureRecognizerStateChanged) {
NSLog(@"长按手势识别成功");
}
if (gestrue.state == UIGestureRecognizerStateEnded) {
NSLog(@"手势识别结束");
}
}
#pragma mark -
#pragma mark UIGestureReconigerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer.view == otherGestureRecognizer.view) {
return YES;
}
return NO;
}