UIGestureRecognizer : 手勢識別器
手勢識別器是一個抽象類, 特殊的觸摸事件. 我們不使用它本身,而是使用它的子類
- 平移 : UIPanGestureRecognizer
- 輕掃 : UISwipeGestureRecognizer
- 長按 : UILongPressGestureRecognizer
- 捏合 : UIPinchGestureRecognizer
- 旋轉(zhuǎn) : UIRotationGestureRecognizer
- 輕拍 : UITapGestureRecognizer
首先, 在視圖控制器中創(chuàng)建一個UIView, 接下來的手勢識別器都會添加到該視圖上進行操作.
UIView *Myview = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
Myview.backgroundColor = [UIColor orangeColor];
[self.view addSubview:Myview];```
######接下來, 初始化響應(yīng)的手勢, 并設(shè)置手勢觸發(fā)的方法, 實現(xiàn)響應(yīng)的效果.
#####1. 輕拍手勢
//1. 初始化輕拍手勢對象, 并設(shè)置輕拍時觸發(fā)的方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//2. 設(shè)置手勢相關(guān)屬性
//輕拍的次數(shù)
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
//3. 將手勢到view對象上
[Myview addGestureRecognizer:tap];```
2. 輕掃手勢
//1. 初始化輕掃手勢對象, 并設(shè)置輕掃時觸發(fā)的方法
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//2. 輕掃方向( | :表示兩個方向都可以)
#輕掃手勢的默認輕掃方向是向右的, 若需要向左掃描時也能觸發(fā)向右的掃描方法, 需要指定掃描方向為以下屬性:
swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
//3. 添加手勢到view對象上
[Myview addGestureRecognizer:swipe];```
#####3. 平移手勢
//1. 初始化平移手勢對象, 并設(shè)置平移時觸發(fā)的方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//2. 將平移手勢添加到View視圖上
[Myview addGestureRecognizer:pan];```
4. 捏合手勢
//1. 創(chuàng)建捏合手勢, 并設(shè)置捏合時觸發(fā)的方法
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
//2. 添加捏合手勢到view視圖上
[Myview addGestureRecognizer:pinch];```
#####5. 旋轉(zhuǎn)手勢
//1. 創(chuàng)建旋轉(zhuǎn)手勢, 并設(shè)置旋轉(zhuǎn)時觸發(fā)的方法
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
//2. 添加手勢到view視圖上
[Myview addGestureRecognizer:rotation];```
<i>注意: 以上多種手勢, 例如輕拍手勢, 我們可以設(shè)置他的輕拍次數(shù), 分別觸發(fā)不同的事件, 但這并沒有那么容易實現(xiàn), 因為模擬器無法分辨輕拍次數(shù), 當(dāng)你輕拍一次的時候, 它默認就會認為應(yīng)該觸發(fā)輕拍次數(shù)為一次的事件.另外, 手勢之間也會發(fā)生沖突, 例如輕拍手勢和輕掃手勢, 模擬器無法分辨. 所以在添加手勢的時候, 盡量避免發(fā)生沖突</i>
第三部分: 實現(xiàn)手勢觸發(fā)的方法
1. 輕拍事件
- (void)tapAction:(UITapGestureRecognizer *)sender{
//通過觸發(fā)輕拍事件, 改變View的背景樣色
sender.view.backgroundColor = [UIColor randomColor];
}```
#####2. 輕掃事件
-
(void)swipeAction:(UISwipeGestureRecognizer *)sender{
sender.view.backgroundColor = [UIColor randomColor];
}```
3. 平移事件
- (void)panAction:(UIPanGestureRecognizer *)sender{
//平移 偏移
CGPoint point = [sender translationInView:sender.view];
//三個參數(shù)
//會根據(jù)上次移動過的位置, 繼續(xù)平移
//參數(shù)一: 代表當(dāng)前位置 參數(shù)二 : 代表移動的x值 參數(shù)三 : 代表移動的Y值
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
//增量置為 0(避免疊加)
[sender setTranslation:CGPointZero inView:sender.view];
}```
#####4. 捏合事件
-
(void)pinchAction:(UIPinchGestureRecognizer *)sender{
//兩個參數(shù)的方法: 每次縮放都會從原始大小開始
//------------------------------------------------------//每次縮放從上一次縮放的大小開始
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);//設(shè)置縮放比例 1 : 是正常比例 < 1 : 縮小 >1 : 放大
sender.scale = 1;
}```
5. 旋轉(zhuǎn)事件
- (void)rotationAction:(UIRotationGestureRecognizer *)sender{
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
//清除增量
sender.rotation = 0;
}```