1.手勢識別器
1.手勢識別器是iOS中比較抽象的一個類氯窍,用于識別一個手勢唆缴,所謂手勢:有規(guī)律的觸摸扑馁。是對觸摸事件做了封裝涯呻,我們無需自己去判斷某個手勢是否觸發(fā)凉驻,手勢識別器本身起到了識別作用,我們把重心放在識別之后要做什么操作上面复罐。
2.手勢識別器有7個子類
分別識別輕拍手勢涝登、平移手勢、輕掃手勢效诅、縮放手勢胀滚、旋轉手勢、長按手勢以及屏幕邊界平移手勢
一旦指定的手勢被識別填帽,我們可以執(zhí)行我們自己定義好的操作
七種手勢.png
3如何使用手勢識別器
我們不會直接使用手勢識別器這個抽象父類蛛淋,而是根據(jù)需要使用特定的手勢識別器創(chuàng)建對象。
1篡腌、創(chuàng)建UIxxxGestureRecognizer對象褐荷,使用initWithTarget:action:方法;
2嘹悼、配置要識別的手勢的相關信息叛甫;
3、將手勢添加到某個視圖上杨伙;
4其监、實現(xiàn)手勢識別器里定義的方法
4.手勢的具體使用
1.1 輕拍手勢
#pragma mark -- 捏合手勢
// 輕拍手指
UITapGestureRecognizer *tagGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 設置觸控對象
[tagGr setNumberOfTouchesRequired:1];
// 設置輕拍的次數(shù)
[tagGr setNumberOfTapsRequired:1];
// 給創(chuàng)建好的視圖添加手勢 (一個視圖可以添加多個手勢,但是一個手勢只能添加到一個視圖上)
[_imageView addGestureRecognizer:tagGr];
1.2輕拍手勢的回調方法
- (void)tapAction:(UITapGestureRecognizer *)sender{
// 輕拍圖片使其變大
UIImageView *imageView = (UIImageView *)sender.view;
CGRect frame = imageView.frame;
frame.size.height = frame.size.height + 5;
frame.size.width = frame.size.width +5;
imageView.frame = frame;
NSLog(@"LOL輕拍了屏幕")
}
2.1捏合手勢
#pragma mark -- 捏合手勢
// 創(chuàng)建捏合手勢
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
pinchGR.delegate = self;
[_imageView addGestureRecognizer:pinchGR];
2.2捏合手勢的回調方法
// 捏合手勢的回調方法
- (void)pinchAction:(UIPinchGestureRecognizer *)sender{
// 通過捏合手勢得到一個縮放比率
float scale = sender.scale;
// 得到該手勢所作用的視圖
UIView *view = sender.view;
// 2D仿射變換函數(shù)中的縮放函數(shù)來實現(xiàn)視圖的放大縮小
// 是在原有基礎上改變當前視圖
// 函數(shù)的第一個參數(shù):現(xiàn)有的視圖的transform值
// 函數(shù)的第二個參數(shù):X軸上的縮放比率
// 函數(shù)的第三個參數(shù):Y軸上的縮放比率
view.transform = CGAffineTransformScale(view.transform, scale,scale);
// 是在視圖最初的transform狀態(tài)上改變的限匣,不管執(zhí)行多少次抖苦,都是以該視圖最初的transform狀態(tài)為基礎來改變
// view.transform = CGAffineTransformMakeScale(0.5, 0.5);
// 每次捏合動作完成值,讓此捏和值復原,使得他每次都從100%開始
sender.scale = 1;
}
3.1旋轉手勢
#pragma mark -- 旋轉手勢
UIRotationGestureRecognizer *rotaGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotaAction:)];
rotaGR.delegate = self;
[_imageView addGestureRecognizer:rotaGR];
3.2旋轉手勢的回調方法
// 旋轉手勢的回調方法
- (void)rotaAction:(UIRotationGestureRecognizer *)sender{
// 通過手勢得到旋轉角度
float rota = sender.rotation;
// 得到手勢作用的視圖
UIView *view = sender.view;
// 通過2D仿射變換函數(shù)中的旋轉函數(shù)來使得當前視圖旋轉
view.transform = CGAffineTransformRotate(view.transform, rota);
// 復原
sender.rotation = 0;
}
4.1平移手勢
#pragma mark -- 平移手勢
UIPanGestureRecognizer *panGP = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
panGP.delegate = self;
[_imageView addGestureRecognizer:panGP];
4.2平移手勢的回調方法
// 平移手勢回調方法
- (void)panAction:(UIPanGestureRecognizer *)sender{
// 得到當前視圖
UIView *view =sender.view;
// 得到我們在視圖上移動的偏移量
CGPoint currentPoint = [sender translationInView:view.superview];
// 通過2D仿射變換函數(shù)中的平移有關函數(shù)使當前視圖位置放生改變
view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x, currentPoint.y);
// 復原
[sender setTranslation:CGPointZero inView:view.superview];
}
5.1清掃手勢
#pragma mark -- 清掃手勢
UISwipeGestureRecognizer *swiGP = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiAction:)];
[_imageView addGestureRecognizer:swiGP];
5.2清掃手勢的回調方法
// 清掃手勢的回調方法
- (void) swiAction:(UIButton *)sender{
NSLog(@"清掃手勢");
}
6.1長按手勢
#pragma mark -- 長按手勢
UILongPressGestureRecognizer *longGP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
[_imageView addGestureRecognizer:longGP];
6.2長按手勢的回調方法
- (void) longAction:(UIButton *)sender{
NSLog(@"長按手勢");
}
7.1邊緣清掃手勢
#pragma mark -- 邊緣輕掃手勢
UIScreenEdgePanGestureRecognizer *edgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(rdgePanAction:)];
[_imageView addGestureRecognizer:edgePanGR];
7.2邊緣清掃手勢的回調方法
// 邊緣清掃手勢的回調方法
- (void)rdgePanAction:(UIScreenEdgePanGestureRecognizer *)sender{
NSLog(@"邊緣清掃手勢C姿馈P坷!");
}
5.手勢的代理
#pragma mark -- 手勢的代理
// 使得多個手勢可以同時響應
- (BOOL )gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
// 返回值是YES的時候峦筒,當執(zhí)行一個手勢的時候究西,也可以執(zhí)行其他手勢
return YES;
}