ViewController.h
//協(xié)議 - 實(shí)現(xiàn)多個(gè)手勢同時(shí)操作
<UIGestureRecognizerDelegate>
//定義視圖對象
UIImageView *_imageView;
//捏合手勢浸踩,可以對視圖放大縮小
UIPinchGestureRecognizer *_pinchGes;
//定義 旋轉(zhuǎn)手勢摇邦,旋轉(zhuǎn)圖片
UIRotationGestureRecognizer *_rotGes;
//平移 手勢
UIPanGestureRecognizer *_pan;
//長按 手勢
UISwipeGestureRecognizer *_swipe;
ViewController.m
//加載圖像對象,從本地加載到內(nèi)存中
UIImage *image = [UIImage imageNamed:@"123"];
//創(chuàng)建圖像視圖
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(34, 164, 306, 217)];
//將圖像視圖賦值
_imageView.image = image;
//按比例縮放节榜。是啥樣就是啥樣吕朵,保持原來的樣子
// _imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:_imageView];
//交互事件響應(yīng)開關(guān)
//YES:可以交互鹦聪,必須要設(shè)置
//NO:不能接收交互若债。默認(rèn)是NO
_imageView.userInteractionEnabled = YES;
#pragma mark - 單擊放大
//創(chuàng)建點(diǎn)擊手勢對象 self:響應(yīng)事件的擁有者,self表示當(dāng)前視圖控制器 action: 響應(yīng)事件的函數(shù)
UITapGestureRecognizer *tapOneGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneAct:)];
//點(diǎn)擊幾次熔任,才會觸發(fā) 默認(rèn)是1
tapOneGes.numberOfTapsRequired = 1;
//幾個(gè)手指褒链,點(diǎn)擊觸發(fā) 默認(rèn)是1
tapOneGes.numberOfTouchesRequired = 1;
//將點(diǎn)擊事件添加到視圖中,視圖即可響應(yīng)事件
[_imageView addGestureRecognizer:tapOneGes];
//單擊放大響應(yīng)函數(shù)
//參數(shù)手勢點(diǎn)擊事件對象
- (void)tapOneAct:(UITapGestureRecognizer *)tap
{
//隨機(jī)背景顏色
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;
//獲取手勢監(jiān)控的視圖對象
UIImageView* imageView = (UIImageView*) tap.view;
//開始動(dòng)畫過程
[UIView beginAnimations:nil context:nil];
//設(shè)置動(dòng)畫過渡時(shí)間
[UIView setAnimationDuration:2];
//縮放布滿整個(gè)屏幕疑苔。 iPhone6 inch-4.7
imageView.frame = CGRectMake(0, 0, 375, 667);
//結(jié)束動(dòng)畫過程
[UIView commitAnimations];
}
#pragma mark - 雙擊縮小
UITapGestureRecognizer *tapTwoGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoAct:)];
tapTwoGes.numberOfTapsRequired = 2;
tapTwoGes.numberOfTouchesRequired = 1;
[_imageView addGestureRecognizer:tapTwoGes];
#pragma bug 在原圖情況下甫匹,會先放大,在縮小惦费。從打印方式可以看出兵迅,雙擊會先出現(xiàn)放大,后出現(xiàn)縮小薪贫。
# 解決方案 ↓
//當(dāng)單擊操作遇到雙擊操作時(shí)恍箭,單擊操作失效
[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(@"如果我有仙女棒,變大變小變漂亮");
//對圖像視圖進(jìn)行矩陣變換計(jì)算并賦值
/*
transform 圖形學(xué)中的變換矩陣
CGAffineTransformScale 通過縮放的方式產(chǎn)生一個(gè)新的矩陣
參數(shù)1 : 原來的矩陣
參數(shù)2 : x方向的縮放比例
參數(shù)3 : y方向的縮放比例
返回值是新的縮放后的矩陣變換
*/
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//將縮放值歸為單位值瞧省,否則會出現(xiàn)累加
/*
scale = 1 原來的大小
scale > 1 放大效果
scale < 1 縮小效果
*/
pinch.scale = 1;
}
#pragma mark - 旋轉(zhuǎn)
_rotGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotAct:)];
[_imageView addGestureRecognizer:_rotGes];
//旋轉(zhuǎn)事件
- (void)rotAct:(UIRotationGestureRecognizer *)rot
{
NSLog(@"看我歪頭");
rot.view.transform = CGAffineTransformRotate(rot.view.transform, rot.rotation);
//選擇角度清零
rot.rotation = 0;
}
# 協(xié)議 - 同時(shí)識別手勢
_rotGes.delegate = self;
_pinchGes.delegate = self;
//Simultaneously 同時(shí)識別手勢 YES: 可以同時(shí)相應(yīng)扯夭,NO: 不可以
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - 移動(dòng)
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAct:)];
[_imageView addGestureRecognizer:_pan];
#這里,為了避免和平掃沖突臀突,所以取消掉移動(dòng)的事件勉抓。
//將移動(dòng)事件從圖像視圖中取消
//[_imageView removeGestureRecognizer:_pan];
//平移 事件 只要手指發(fā)生變化,函數(shù)就被調(diào)用
- (void)panAct:(UIPanGestureRecognizer *)pan
{
// NSLog(@"111111111111");
// //獲取移動(dòng)的坐標(biāo)候学,相對于視圖的坐標(biāo)而言
// CGPoint pt = [pan translationInView:self.view];
// NSLog(@"pt.x = %.2f , pt.y = %.2f",pt.x,pt.y);
//
// //獲取移動(dòng)時(shí)的 相對速度 每秒在屏幕移動(dòng)像素的值
// CGPoint pv = [pan velocityInView:self.view];
// NSLog(@"pv.x = %.2f , pv.y = %.2f",pv.x,pv.y);
//在pan.view上移動(dòng)的距離
CGPoint translation = [pan translationInView:pan.view];
//view的中心
CGPoint center = pan.view.center;
//中心點(diǎn).x + 移動(dòng)距離.x
center.x = center.x + translation.x;
center.y = center.y + translation.y;
pan.view.center = center;
//清空移動(dòng)距離
[pan setTranslation:CGPointZero inView:pan.view];
}
#pragma mark - 滑動(dòng)(平掃)
_swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAct:)];
/*
UISwipeGestureRecognizerDirection
Right 向右
Left 向左
Up 向上
Down 向下
*/
// | 位或
_swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:_swipe];
//滑動(dòng) 事件
- (void)swipeAct:(UISwipeGestureRecognizer *)swipe
{
// NSLog(@"向左滑動(dòng)");
#pragma mark - 有問題 ,不能分別打印
if (_swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左滑動(dòng)");
}else{
NSLog(@"向右滑動(dòng)");
}
}
#pragma mark - 長按
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressLong:)];
//設(shè)置長按手勢的時(shí)間纵散,默認(rèn)是0.5
press.minimumPressDuration = 3;
[_imageView addGestureRecognizer:press];
//長按事件
- (void)pressLong:(UILongPressGestureRecognizer *)press
{
//手勢的狀態(tài)對象梳码,到達(dá)規(guī)定時(shí)間(3s)隐圾,觸發(fā)函數(shù)
if (press.state == UIGestureRecognizerStateBegan) {
NSLog(@"開始!");
}
//當(dāng)手指離開時(shí)掰茶,結(jié)束
else if (press.state == UIGestureRecognizerStateEnded)
{
NSLog(@"結(jié)束暇藏!");
}
NSLog(@"長按");
}