IOS 手勢
簡介:
1.常見手勢
手勢說明:
- Tap(點一下)
- Pinch(二指往內或往外撥動,平時經常用到的縮放)
- Rotation(旋轉)
- Swipe(滑動,快速移動)
- Pan (拖移,慢速移動)
- LongPress(長按)
使用手勢的步驟
使用手勢很簡單,分為兩步:
- 創建手勢實例。當創建手勢時,指定一個回調方法,當手勢開始,改變、或結束時,回調方法被調用。
- 添加到需要識別的View中。每個手勢只對應一個View,當屏幕觸摸在View的邊界內時,如果手勢和預定的一樣,那就會回調方法。
ps:一個手勢只能對應一個View,但是一個View可以有多個手勢。
UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]];
snakeImageView.frame = CGRectMake(50, 50, 100, 160);
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[snakeImageView addGestureRecognizer:panGestureRecognizer];
- Tap(點一下)
- Pinch(二指往內或往外撥動,平時經常用到的縮放)
- Rotation(旋轉)
這三種手勢不用細講比較簡單看下demo即可
講下以下幾點:
- Pan拖動手勢速度與拖動結束狀態
- 同時觸發兩個view的手勢
- 解決手勢依耐性 也叫手勢沖突
- 自定義手勢
1.Pan拖動手勢速度與拖動結束狀態
狀態
recognizer.state == UIGestureRecognizerStateEnded
>速度
>~~~
CGPoint velocity = [recognizer velocityInView:self.view];
2.同時觸發兩個view的手勢
手勢之間是互斥的,如果你想同時觸發View1和View2的手勢,比如說(1)view1 拖動 View2 拖動 或者 (2)view1 拖動 View 縮放 正常情況只有一個手勢會響應。那么需要實現協議
UIGestureRecognizerDelegate。
1.添加代理
//<UIGestureRecognizerDelegate>
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
@end
panGestureRecognizer.delegate = self;
pinchGestureRecognizer.delegate = self;
rotateRecognizer.delegate = self;
2.實現代理
添加代理的手勢將會進入這個方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Simultaneously//同時的
3.解決手勢依耐性 也叫手勢沖突
比較典型的例子:
pan 拖動手勢和 tap 點擊手勢
pan拖動手勢 通常會觸發tap 手勢,使得pan 和tap 手勢事件同時發生,而使用時往往我們只希望響應生效的手勢,可以用函數 requireGestureRecognizerToFail 來解決手勢沖突問題
[tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];
意思就是,當如果pan手勢失敗,就是沒發生拖動,才會出發tap手勢。這樣如果你有輕微的拖動,那就是pan手勢發生了。tap的聲音就不會發出來了
4.自定義手勢
自定義手勢繼承:UIGestureRecognizer,實現下面的方法:
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
- touchesCancelled:withEvent:
例子:
.h文件
#import <UIKit/UIKit.h>
typedef enum {
DirectionUnknown = 0,
DirectionLeft,
DirectionRight
} Direction;
@interface HappyGestureRecognizer : UIGestureRecognizer
@property (assign) int tickleCount; //撓癢次數
@property (assign) CGPoint curTickleStart; //獲取開始點
@property (assign) Direction lastDirection;//最近一次方向
@end
.m文件
#import "HappyGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#define REQUIRED_TICKLES 2
#define MOVE_AMT_PER_TICKLE 25
@implementation HappyGestureRecognizer
//手勢開始時記錄開始坐標點
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
self.curTickleStart = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// Make sure we've moved a minimum amount since curTickleStart
UITouch * touch = [touches anyObject];
CGPoint ticklePoint = [touch locationInView:self.view]; //獲取點擊點坐標
CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x; //現在點擊坐標 與 初始坐標
Direction curDirection;
if (moveAmt < 0) {
curDirection = DirectionLeft;
} else {
curDirection = DirectionRight;
}
if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
// 確認方向改變了
if (self.lastDirection == DirectionUnknown ||
(self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
(self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
// 撓癢次數
self.tickleCount++;
self.curTickleStart = ticklePoint;
self.lastDirection = curDirection;
// 一旦撓癢次數超過指定數,設置手勢為結束狀態
// 這樣回調函數會被調用。
if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
[self setState:UIGestureRecognizerStateEnded];
}
}
}
//重置相關數據
- (void)reset {
self.tickleCount = 0;
self.curTickleStart = CGPointZero;
self.lastDirection = DirectionUnknown;
if (self.state == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self reset];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self reset];
}
@end
調用自定義手勢和系統手勢一樣
- (void)handleHappy:(HappyGestureRecognizer *)recognizer{
[self.hehePlayer play];
}