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