ViewController.h
//协议 - 实现多个手势同时操作
<UIGestureRecognizerDelegate>
//定义视图对象
UIImageView *_imageView;
//捏合手势,可以对视图放大缩小
UIPinchGestureRecognizer *_pinchGes;
//定义 旋转手势,旋转图片
UIRotationGestureRecognizer *_rotGes;
//平移 手势
UIPanGestureRecognizer *_pan;
//长按 手势
UISwipeGestureRecognizer *_swipe;
ViewController.m
//加载图像对象,从本地加载到内存中
UIImage *image = [UIImage imageNamed:@"123"];
//创建图像视图
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(34, 164, 306, 217)];
//将图像视图赋值
_imageView.image = image;
//按比例缩放。是啥样就是啥样,保持原来的样子
// _imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:_imageView];
//交互事件响应开关
//YES:可以交互,必须要设置
//NO:不能接收交互。默认是NO
_imageView.userInteractionEnabled = YES;
#pragma mark - 单击放大
//创建点击手势对象 self:响应事件的拥有者,self表示当前视图控制器 action: 响应事件的函数
UITapGestureRecognizer *tapOneGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneAct:)];
//点击几次,才会触发 默认是1
tapOneGes.numberOfTapsRequired = 1;
//几个手指,点击触发 默认是1
tapOneGes.numberOfTouchesRequired = 1;
//将点击事件添加到视图中,视图即可响应事件
[_imageView addGestureRecognizer:tapOneGes];
//单击放大响应函数
//参数手势点击事件对象
- (void)tapOneAct:(UITapGestureRecognizer *)tap
{
//随机背景颜色
NSLog(@"放大");
// float randomR = arc4random_uniform(255)/255.0;
// float randomG = arc4random_uniform(255)/255.0;
// float randomB = arc4random_uniform(255)/255.0;
//
// UIColor *randomColor = [UIColor colorWithRed:randomR green:randomG blue:randomB alpha:1];
//
// self.view.backgroundColor = randomColor;
//获取手势监控的视图对象
UIImageView* imageView = (UIImageView*) tap.view;
//开始动画过程
[UIView beginAnimations:nil context:nil];
//设置动画过渡时间
[UIView setAnimationDuration:2];
//缩放布满整个屏幕。 iPhone6 inch-4.7
imageView.frame = CGRectMake(0, 0, 375, 667);
//结束动画过程
[UIView commitAnimations];
}
#pragma mark - 双击缩小
UITapGestureRecognizer *tapTwoGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoAct:)];
tapTwoGes.numberOfTapsRequired = 2;
tapTwoGes.numberOfTouchesRequired = 1;
[_imageView addGestureRecognizer:tapTwoGes];
#pragma bug 在原图情况下,会先放大,在缩小。从打印方式可以看出,双击会先出现放大,后出现缩小。
# 解决方案 ↓
//当单击操作遇到双击操作时,单击操作失效
[tapOneGes requireGestureRecognizerToFail:tapTwoGes];
//双击缩小事件
- (void)tapTwoAct:(UITapGestureRecognizer *)tap
{
NSLog(@"缩小");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
_imageView.frame = CGRectMake(34, 164, 306, 217);
[UIView commitAnimations];
}
#pragma mark - 捏合 放大缩小
_pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
//将捏合手势添加到视图上去
[_imageView addGestureRecognizer:_pinchGes];
//捏合-放大缩小事件 按住alt键
- (void)pinchAct:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"如果我有仙女棒,变大变小变漂亮");
//对图像视图进行矩阵变换计算并赋值
/*
transform 图形学中的变换矩阵
CGAffineTransformScale 通过缩放的方式产生一个新的矩阵
参数1 : 原来的矩阵
参数2 : x方向的缩放比例
参数3 : y方向的缩放比例
返回值是新的缩放后的矩阵变换
*/
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//将缩放值归为单位值,否则会出现累加
/*
scale = 1 原来的大小
scale > 1 放大效果
scale < 1 缩小效果
*/
pinch.scale = 1;
}
#pragma mark - 旋转
_rotGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotAct:)];
[_imageView addGestureRecognizer:_rotGes];
//旋转事件
- (void)rotAct:(UIRotationGestureRecognizer *)rot
{
NSLog(@"看我歪头");
rot.view.transform = CGAffineTransformRotate(rot.view.transform, rot.rotation);
//选择角度清零
rot.rotation = 0;
}
# 协议 - 同时识别手势
_rotGes.delegate = self;
_pinchGes.delegate = self;
//Simultaneously 同时识别手势 YES: 可以同时相应,NO: 不可以
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - 移动
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAct:)];
[_imageView addGestureRecognizer:_pan];
#这里,为了避免和平扫冲突,所以取消掉移动的事件。
//将移动事件从图像视图中取消
//[_imageView removeGestureRecognizer:_pan];
//平移 事件 只要手指发生变化,函数就被调用
- (void)panAct:(UIPanGestureRecognizer *)pan
{
// NSLog(@"111111111111");
// //获取移动的坐标,相对于视图的坐标而言
// CGPoint pt = [pan translationInView:self.view];
// NSLog(@"pt.x = %.2f , pt.y = %.2f",pt.x,pt.y);
//
// //获取移动时的 相对速度 每秒在屏幕移动像素的值
// CGPoint pv = [pan velocityInView:self.view];
// NSLog(@"pv.x = %.2f , pv.y = %.2f",pv.x,pv.y);
//在pan.view上移动的距离
CGPoint translation = [pan translationInView:pan.view];
//view的中心
CGPoint center = pan.view.center;
//中心点.x + 移动距离.x
center.x = center.x + translation.x;
center.y = center.y + translation.y;
pan.view.center = center;
//清空移动距离
[pan setTranslation:CGPointZero inView:pan.view];
}
#pragma mark - 滑动(平扫)
_swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAct:)];
/*
UISwipeGestureRecognizerDirection
Right 向右
Left 向左
Up 向上
Down 向下
*/
// | 位或
_swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:_swipe];
//滑动 事件
- (void)swipeAct:(UISwipeGestureRecognizer *)swipe
{
// NSLog(@"向左滑动");
#pragma mark - 有问题 ,不能分别打印
if (_swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左滑动");
}else{
NSLog(@"向右滑动");
}
}
#pragma mark - 长按
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressLong:)];
//设置长按手势的时间,默认是0.5
press.minimumPressDuration = 3;
[_imageView addGestureRecognizer:press];
//长按事件
- (void)pressLong:(UILongPressGestureRecognizer *)press
{
//手势的状态对象,到达规定时间(3s),触发函数
if (press.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始!");
}
//当手指离开时,结束
else if (press.state == UIGestureRecognizerStateEnded)
{
NSLog(@"结束!");
}
NSLog(@"长按");
}