Gesture手勢

手勢識別器

手勢識別器是對觸摸事件做了封裝,我們無需自己去判斷某個手勢是否觸發,手勢識別器本身起到了識別作用,我們把重心放在識別之后要做什么操作上面。手勢識別器是iOS中比較抽象的一個類,用于識別一個手勢。
手勢識別器有7個子類:輕拍手勢、平移手勢、輕掃手勢、縮放手勢、旋轉手勢、長按手勢以及屏幕邊界平移手勢。一旦指定的手勢被識別,我們可以執行我們自己定義好的操作。

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:)];
//給創建好的視圖添加手勢
[gestureView addGestureRecognizer:tapGR];
[tapGR setNumberOfTapsRequired:2];//設置輕怕次數
tapGR.numberOfTouchesRequired = 1;//設置輕怕次數```
```//輕掃手勢的回調方法
- (void)swipeAction: (UISwipeGestureRecognizer *)sender
{
    NSLog(@"123");
    //創建一個警示框
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"內容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
    [alertView show];//展示警示框
    
}```

##UILongPressGestureRecognizer
長按手勢識別器,能識別長按操作。
```//長按手勢
UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
[gestureView addGestureRecognizer:longPGR];```
```//長按手勢的回調方法
- (void)longAction: (UILongPressGestureRecognizer *)sender
{
    //直接聲明,長按手勢的回調方法會執行兩次,因為長按狀態下有開始和結束兩種狀態,如果不加判斷,系統會認為結束也就是另一個長按的開始,所以加一句在長按結束時再執行,開始時不執行
    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];```
```//捏合手勢的回調方法
- (void)pinchAction: (UIPinchGestureRecognizer *)sender
{
    //通過捏合手勢得到縮放比率
    float scale = sender.scale;
    //得到該手勢得到的視圖
    UIView *view = sender.view;
    //2D仿射變換函數的縮放函數來實現視圖的放大縮小
    //是在原有基礎上來改變當前的視圖
    /**
     *  @param t#>  <#t#> description#> 現有的視圖的transform
     *  @param sx#> <#sx#> description#> x軸上的縮放比率
     *  @param sy#> <#sy#> description#> y軸上的縮放比率
     *
     */
    view.transform = CGAffineTransformScale(view.transform, scale, scale);
    //是在視圖最初的tramsfrom狀態上改變,不管執行多少次,都是以該視圖最初的transform狀態為基礎來改變
    //每次捏合動作完畢之后,讓此次捏合值復原,使得它每次捏合都是從1倍開始
    sender.scale = 1;
}```

##UIRotationGestureRecognizer
旋轉手勢識別器,能識別旋轉操作。
```//旋轉手勢
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[gestureView addGestureRecognizer:rotationGR];
- (void)rotationAction: (UIRotationGestureRecognizer *)sender
{
    //通過手勢得到旋轉角度
    float rota = sender.rotation;
    //得到該手勢作用的視圖
    UIView *view = sender.view;
    //通過2D仿射變換函數中的旋轉函數來使得當前視圖旋轉
    view.transform = CGAffineTransformRotate(view.transform, rota);
    //復原
    sender.rotation = 0; 
}```

##UIPanGestureRecognizer
平移手勢識別器,能識別拖拽操作。
```//平移手勢
UIPanGestureRecognizer *panGP =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[gestureView addGestureRecognizer:panGP];```
```//平移手勢的回調方法
- (void)panAction: (UIPanGestureRecognizer *)sender
{
    //得到手勢當前所在視圖
    UIImageView *imageView = (UIImageView *)sender.view;
    //得到手勢在視圖上移動的偏移量
    CGPoint late = [sender translationInView:imageView.superview];
    //通過2D方式變換函數中的平移函數使得當前函數平移
    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];```
```//輕掃手勢的回調方法
- (void)swipeAction: (UISwipeGestureRecognizer *)sender
{
    NSLog(@"實現了輕掃手勢");
}```

##UIScreenEdgePanGestureRecognizer
屏幕邊緣輕掃識別器,是iOS7中新增的手勢。
```//邊緣輕掃手勢
UIScreenEdgePanGestureRecognizer *screenEdgePanGr = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction:)];
screenEdgePanGr.edges = UIRectEdgeAll;
[gestureView addGestureRecognizer:screenEdgePanGr];```
```- (void)edgeAction: (UIScreenEdgePanGestureRecognizer *)sender
{
    NSLog(@"成功觸發了屏幕邊緣手勢");
}```

#使得多個手勢可以同時響應的代理方法(必須對手勢指定代理)
```- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    //返回值為yes的時候,當執行一個手勢的操作的時候,也可以執行其它手勢的操作
    return YES;
}```


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容