手勢識別器
手勢識別器是對觸摸事件做了封裝,我們無需自己去判斷某個手勢是否觸發(fā)子眶,手勢識別器本身起到了識別作用之碗,我們把重心放在識別之后要做什么操作上面。手勢識別器是iOS中比較抽象的一個類失晴,用于識別一個手勢。
手勢識別器有7個子類:輕拍手勢拘央、平移手勢涂屁、輕掃手勢、縮放手勢堪滨、旋轉(zhuǎn)手勢胯陋、長按手勢以及屏幕邊界平移手勢。一旦指定的手勢被識別袱箱,我們可以執(zhí)行我們自己定義好的操作遏乔。
UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
gestureView.center = self.window.center;//將當前視圖放置在window中間
gestureView.backgroundColor = [UIColor grayColor];//設置背景色
[self.window addSubview:gestureView];//添加到window上```
##UITapGestureRecognizer
輕拍手勢識別器,能識別輕拍操作
```//輕拍手勢
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//給創(chuàng)建好的視圖添加手勢
[gestureView addGestureRecognizer:tapGR];
[tapGR setNumberOfTapsRequired:2];//設置輕怕次數(shù)
tapGR.numberOfTouchesRequired = 1;//設置輕怕次數(shù)```
```//輕掃手勢的回調(diào)方法
- (void)swipeAction: (UISwipeGestureRecognizer *)sender
{
NSLog(@"123");
//創(chuàng)建一個警示框
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"內(nèi)容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alertView show];//展示警示框
}```
##UILongPressGestureRecognizer
長按手勢識別器发笔,能識別長按操作盟萨。
```//長按手勢
UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
[gestureView addGestureRecognizer:longPGR];```
```//長按手勢的回調(diào)方法
- (void)longAction: (UILongPressGestureRecognizer *)sender
{
//直接聲明,長按手勢的回調(diào)方法會執(zhí)行兩次了讨,因為長按狀態(tài)下有開始和結(jié)束兩種狀態(tài)捻激,如果不加判斷,系統(tǒng)會認為結(jié)束也就是另一個長按的開始前计,所以加一句在長按結(jié)束時再執(zhí)行胞谭,開始時不執(zhí)行
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"000");
}
else if (sender.state == UIGestureRecognizerStateBegan)
{
NSLog(@"123");
}
}```
##UIPinchGestureRecognizer
捏合手勢識別器,能識別捏合操作男杈。
```//捏合手勢
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[gestureView addGestureRecognizer:pinchGR];```
```//捏合手勢的回調(diào)方法
- (void)pinchAction: (UIPinchGestureRecognizer *)sender
{
//通過捏合手勢得到縮放比率
float scale = sender.scale;
//得到該手勢得到的視圖
UIView *view = sender.view;
//2D仿射變換函數(shù)的縮放函數(shù)來實現(xiàn)視圖的放大縮小
//是在原有基礎上來改變當前的視圖
/**
* @param t#> <#t#> description#> 現(xiàn)有的視圖的transform
* @param sx#> <#sx#> description#> x軸上的縮放比率
* @param sy#> <#sy#> description#> y軸上的縮放比率
*
*/
view.transform = CGAffineTransformScale(view.transform, scale, scale);
//是在視圖最初的tramsfrom狀態(tài)上改變丈屹,不管執(zhí)行多少次,都是以該視圖最初的transform狀態(tài)為基礎來改變
//每次捏合動作完畢之后伶棒,讓此次捏合值復原旺垒,使得它每次捏合都是從1倍開始
sender.scale = 1;
}```
##UIRotationGestureRecognizer
旋轉(zhuǎn)手勢識別器,能識別旋轉(zhuǎn)操作肤无。
```//旋轉(zhuǎn)手勢
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[gestureView addGestureRecognizer:rotationGR];
- (void)rotationAction: (UIRotationGestureRecognizer *)sender
{
//通過手勢得到旋轉(zhuǎn)角度
float rota = sender.rotation;
//得到該手勢作用的視圖
UIView *view = sender.view;
//通過2D仿射變換函數(shù)中的旋轉(zhuǎn)函數(shù)來使得當前視圖旋轉(zhuǎn)
view.transform = CGAffineTransformRotate(view.transform, rota);
//復原
sender.rotation = 0;
}```
##UIPanGestureRecognizer
平移手勢識別器先蒋,能識別拖拽操作。
```//平移手勢
UIPanGestureRecognizer *panGP =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[gestureView addGestureRecognizer:panGP];```
```//平移手勢的回調(diào)方法
- (void)panAction: (UIPanGestureRecognizer *)sender
{
//得到手勢當前所在視圖
UIImageView *imageView = (UIImageView *)sender.view;
//得到手勢在視圖上移動的偏移量
CGPoint late = [sender translationInView:imageView.superview];
//通過2D方式變換函數(shù)中的平移函數(shù)使得當前函數(shù)平移
imageView.transform = CGAffineTransformTranslate(imageView.transform, late.x, late.y);
[sender setTranslation:CGPointZero inView:imageView.superview];
}```
##UISwipeGestureRecognizer
輕掃手勢識別器宛渐,能識別拖拽操作竞漾。
```//輕掃手勢
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//設置輕掃的方向,默認值是從左往右窥翩,最多同時支持(左右)或者(上下)
swipeGR.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
[gestureView addGestureRecognizer:swipeGR];```
```//輕掃手勢的回調(diào)方法
- (void)swipeAction: (UISwipeGestureRecognizer *)sender
{
NSLog(@"實現(xiàn)了輕掃手勢");
}```
##UIScreenEdgePanGestureRecognizer
屏幕邊緣輕掃識別器畴蹭,是iOS7中新增的手勢。
```//邊緣輕掃手勢
UIScreenEdgePanGestureRecognizer *screenEdgePanGr = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction:)];
screenEdgePanGr.edges = UIRectEdgeAll;
[gestureView addGestureRecognizer:screenEdgePanGr];```
```- (void)edgeAction: (UIScreenEdgePanGestureRecognizer *)sender
{
NSLog(@"成功觸發(fā)了屏幕邊緣手勢");
}```
#使得多個手勢可以同時響應的代理方法(必須對手勢指定代理)
```- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
//返回值為yes的時候鳍烁,當執(zhí)行一個手勢的操作的時候叨襟,也可以執(zhí)行其它手勢的操作
return YES;
}```