//ios 七种手势简单总结
{
UIImage *image = [UIImage imageNamed:@""];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
= CGPointMake(/2, /2);
= image;
//开启用户交互,默认是关闭的
[imageView setUserInteractionEnabled:YES];
[ addSubview:imageView];
//1.长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]init];
= 2;//连续点击的次数
= 0;//长按时间
= 2;//触摸的次数(相当于几个手指头在上面)
= 10;//触摸过程中是否允许移动
[longPress addTarget:self action:@selector(longPress:)];
[imageView addGestureRecognizer:longPress];
//2.平移手势
UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]init];
// = 1;// 最少手指个数)
= 2;// 最多手指个数
[panGes addTarget:self action:@selector(panGes:)];
[imageView addGestureRecognizer:panGes];
//3.捏合手势
UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc]init];
[pinGes addTarget:self action:@selector(pinGes:)];
//可以在一个视图上添加多个手势
// = self;
[imageView addGestureRecognizer:pinGes];
//4.旋转手势
UIRotationGestureRecognizer *roatGes = [[UIRotationGestureRecognizer alloc]init];
[roatGes addTarget:self action:@selector(roatGes:)];
[imageView addGestureRecognizer:roatGes];
//轻扫手势
UISwipeGestureRecognizer *swipeGes = [[UISwipeGestureRecognizer alloc]init];
[swipeGes addTarget:self action:@selector(swipeGes:)];
= 1;//需要点击几次(需要几个手指)
[imageView addGestureRecognizer:swipeGes];
//轻拍手势
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]init];
= 1;//几个手势
= 1;//设置轻拍几次
[tapGes addTarget:self action:@selector(tapGes:)];
[imageView addGestureRecognizer:tapGes];
//边缘轻扫
UIScreenEdgePanGestureRecognizer *sceenGes = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(sceenGes:)];
= UIRectEdgeRight;
[ addGestureRecognizer:sceenGes];
}
- (void)sceenGes:(UIScreenEdgePanGestureRecognizer*)sceenGes{
NSLog(@"我成功的触发了屏幕边缘手势");
}
- (void)tapGes:(UITapGestureRecognizer *)tapGes {
NSLog(@"轻拍了视图");
//可以根据手势获取得到当前他所作用的视图,来操作所作用的视图
UIImageView *view = (UIImageView *);
= CGRectMake(0, 0, 300, 300);
}
- (void)swipeGes:(UISwipeGestureRecognizer *)swipeGes {
NSLog(@"轻扫");
}
- (void)roatGes:(UIRotationGestureRecognizer *)roatGes {
NSLog(@"旋转");
//通过手势的旋转得到选择的角度
float rota = ;
//首选获取旋转视图的视图进行操作
UIView *view = ;
//通过2D仿射变换函数中的旋转函数来是得当前视图选择
= CGAffineTransformRotate(, rota);
//复原
= 0;
}
- (void)pinGes:(UIPinchGestureRecognizer *)pinGes {
NSLog(@"捏合");
//通过捏合手势的到缩放比率
float scale = ;
//获取捏合手势的视图进行操作视图
UIView *view = ;
//2D仿射视图变换函数中的缩放函数来实现视图的放大缩小
//是在原有基础上来改变当前的视图
//函数的第一个参数:现有的视图transform值
//第二个参数:X轴上的缩放比率
//第三个参数:Y轴上的缩放比率
= CGAffineTransformScale(, scale, scale);
//每次捏合动作完毕之后,让此捏合值复原,使得它每次都是从100%开始缩放
= 1;
}
- (void)panGes:(UIPanGestureRecognizer *)panGes {
NSLog(@"平移");
//想要让视图平移,首先需要得到当前手势所在的视图,然后获取视图平移的偏移量,最后来实现视图平移后的位置
UIView *view = ;
//得到我们在视图上移动的偏移量 返回在横坐标是,纵坐标上拖动了多少个像素
CGPoint currenPoint = [panGes translationInView:];
//得到在指定坐标上panGes拖动的速度
// CGPoint velocity = [panGes velocityInView:view];
//通过2D仿射变换函数中与位移有关的函数实现视图位置变化
= CGAffineTransformTranslate(, , );
//复原每次都是从00点开始
[panGes setTranslation:CGPointZero inView:];
}
当一个视图上想要添加多种手势的时候就要用到手势的代理(重点)
pragma mark ----手势的代理方法
// 使得多个手势可以同时响应
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
// 返回值为YES的时候,当执行一个手势的操作的时候,也可以执行其他手势的操作
return YES;
}
//技术有限如有不当之处请指出,O(∩_∩)O谢谢