UIGestureRecognize手勢(shì)識(shí)別器的使用簡(jiǎn)介
-
手勢(shì)識(shí)別器是一個(gè)抽象類, 特殊的觸摸事件. 我們不使用它本身,而是使用它的子類
類型 | 類 名|
:-----:||:------|
平移 | UIPanGestureRecognizer
輕掃(滑動(dòng)) | UISwipeGestureRecognizer
長(zhǎng)按 | UILongPressGestureRecognizer
捏合 | UIPinchGestureRecognizer
旋轉(zhuǎn) | UIRotationGestureRecognizer
輕拍 | UITapGestureRecognizer
下面結(jié)合代碼一一解讀:
- 首先如果你想實(shí)現(xiàn)同時(shí)觸發(fā)手勢(shì)(捏合旋轉(zhuǎn)等)的作用,需要實(shí)現(xiàn)下面的協(xié)議
<UIGestureRecognizerDelegate>
方法(其他方法可以進(jìn)去看下)
# Simultaneously : 同時(shí)識(shí)別手勢(shì)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}```
-------------
這里我都把手勢(shì)都添加到自定義的一個(gè) UImageView 上面
--------------
--------
####輕拍手勢(shì):
- 第一步:創(chuàng)建并添加
```code
UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
// 手勢(shì)添加手勢(shì)事件
[tap addTarget:self action:@selector(tapView:)];
# 關(guān)鍵屬性: 設(shè)置輕拍的次數(shù):
tap.numberOfTapsRequired = 1;
// 將輕拍的手勢(shì)添加到 UIimageView 上
[self.imageView addGestureRecognizer:tap];
- 第二步: 實(shí)現(xiàn)手勢(shì)事件
-(void)tapView:(UITapGestureRecognizer *)tap
{
NSLog(@"我被輕輕的拍了~~~我要換圖片了~~~~");
self.imgeView.image = [UIImage imageNamed:@"2.jpg"];
}
長(zhǎng)按手勢(shì):
- 第一步: 創(chuàng)建并添加到 ImageView 上
UILongPressGestureRecognizer *longPress = [UILongPressGestureRecognizer new];
[longPress addTarget:self action:@selector(longPress)]:
# 關(guān)鍵屬性: 至少按0.5秒才會(huì)有反應(yīng)
longPress.minimumPressDuration = 0.5;
[self.imageView addGestureRecognizer:longPress];```
- 第二步: 實(shí)現(xiàn)長(zhǎng)按手勢(shì)事件
```code
-(void)longPress
{
NSLog(@"你在長(zhǎng)按著我~~我的背景要變色");
self.imageView.backgroundColor = [UIColor redColor];
}```
----------
####平移手勢(shì):
- 第一步: 創(chuàng)建并添加
```code
UIPanGestureRecognizer *pan = [UIPanGestureRecognizer new];
[pan addTarget:self action:@selector(panView:)];
//將手勢(shì)添加到圖片上
[self.imageView addGestureRecognizer:pan];```
第二步: 實(shí)現(xiàn)平移事件: 這里我們讓照片跟著手指的平移一起移動(dòng)
```code
-(void)panView:(UIPanGestureRecognizer *)pan
{
#這里有個(gè)區(qū)別
#1 locationInView:獲取到的是手指點(diǎn)擊屏幕實(shí)時(shí)的坐標(biāo)點(diǎn)馒稍;
#2 translationInView:獲取到的是手指移動(dòng)后,在相對(duì)坐標(biāo)中的偏移量
//在pan.view上移動(dòng)的距離
CGPoint translation = [pan translationInView:pan.view];
//獲取View的中心點(diǎn)
CGPoint center = pan.view.center;
//中心點(diǎn)的x值+挪動(dòng)距離的x值
center.x = center.x + translation.x;
//中心點(diǎn)的y值+挪動(dòng)距離的y值
center.y = center.y+translation.y;
pan.view.center = center;
//清空移動(dòng)距離
[pan setTranslation:CGPointZero inView:pan.view];
}```
效果展示:
![平移手勢(shì)效果圖](http://upload-images.jianshu.io/upload_images/1523603-92daddc8089597aa.gif?imageMogr2/auto-orient/strip)
------------------
####旋轉(zhuǎn)手勢(shì):
- 第一步: 創(chuàng)建并添加
```code
UIRotationGestureRecognizer *ritation = [UIRotationGestureRecognizer new];
[ritation addTarget:self action:@selector(ritationView:)];
//將手勢(shì)放到圖片上
[self.imageView addGestureRecognizer:ritation];
- 第二步: 實(shí)現(xiàn)旋轉(zhuǎn)事件 這里我們讓照片跟著一起旋轉(zhuǎn)
-(void)ritationView:(UIRotationGestureRecognizer *)rotation
{
# 這是一個(gè)二維坐標(biāo)轉(zhuǎn)換的結(jié)構(gòu)體數(shù)據(jù)
/*
struct CGAffineTransform {
CGFloat a, b, c, d;
CGFloat tx, ty;
};
其中a,d表示放大縮小有關(guān)
b,c表示的是選擇角度有關(guān)
tx,ty是和平移有關(guān)*/
# 這句意思是 手勢(shì)所在視圖的轉(zhuǎn)化形式是
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
# 每轉(zhuǎn)一次要把角度調(diào)回到0 要不然下次旋轉(zhuǎn)家督實(shí)在上次的基礎(chǔ)之上
rotation.rotation = 0;//rotation:代表的是旋轉(zhuǎn)的弧度
NSLog(@"旋轉(zhuǎn)~~~轉(zhuǎn)啊轉(zhuǎn)~~~");
}
效果圖:
- 第一步: 創(chuàng)建并添加
UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer new];
[pinch addTarget:self action:@selector(pinchView:)];
//將手勢(shì)添加到圖片之上
[self.imageView addGestureRecognizer:pinch];
第二步: 實(shí)現(xiàn)捏合事件 這里我們讓圖片隨著捏合縮放
-(void)pinchView:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合,捏一捏~~~ 我正在被捏~~~");
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//scale 相對(duì)于屏幕來(lái)說(shuō)的坐標(biāo)點(diǎn)
pinch.scale = 1;
}```
效果示意圖:
![捏合縮放圖片示意圖](http://upload-images.jianshu.io/upload_images/1523603-d1eec01acbd6d12c.gif?imageMogr2/auto-orient/strip)
-------------------
####清掃(滑動(dòng))手勢(shì):
- 第一步: 創(chuàng)建并添加
```code
UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction:)];
# 注意重要屬性: 這個(gè)手勢(shì) 默認(rèn)屬性direction(方向)只有向右滑動(dòng) 所以要為左滑動(dòng)更改下屬性 向右是默認(rèn) 可以不改
left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageView addGestureRecognizer:left];```
- 第二步: 實(shí)現(xiàn)事件
```code
- (void)leftAction:(UISwipeGestureRecognizer *)sender
{
NSLog(@"向右滑動(dòng)了");
}```
[結(jié)合動(dòng)畫動(dòng)畫清掃(滑動(dòng))手勢(shì)實(shí)例:](http://www.reibang.com/p/a730199dd12b)
![輕掃滑動(dòng)結(jié)合動(dòng)畫實(shí)現(xiàn)效果演示](http://upload-images.jianshu.io/upload_images/1523603-96990bce6122f773.gif?imageMogr2/auto-orient/strip)