手勢是 iOS 互動設計的靈魂。系統在 UIGestureRecognizer 體系下提供了六大類手勢,掌握它們的屬性與代理回調,絕大部分互動需求都能搞定。
一、系統提供的六種手勢
UITapGestureRecognizer:點擊、敲擊UILongPressGestureRecognizer:長按UIPinchGestureRecognizer:捏合縮放UIRotationGestureRecognizer:旋轉UISwipeGestureRecognizer:輕掃UIPanGestureRecognizer:拖拽二、兩個關鍵代理方法
手勢的代理協議是 UIGestureRecognizerDelegate,其中兩個方法最常用:
第一個決定手勢是否具備觸發條件,回傳 NO 可以攔下手勢,讓它完全不生效:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
第二個決定多個手勢能否同時辨識,例如縮放與旋轉同時進行:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
三、點擊手勢 UITapGestureRecognizer
兩個核心屬性:numberOfTapsRequired 設定需要點擊幾次,numberOfTouchesRequired 設定需要幾根手指。
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tap.delegate = self; tap.numberOfTapsRequired = 1; tap.numberOfTouchesRequired = 1; [self.image addGestureRecognizer:tap];
四、長按手勢 UILongPressGestureRecognizer
minimumPressDuration 是判定長按的最短按壓時長,按下到抬起的間隔若小於此值則不算長按;allowableMovement 則限制長按期間手指允許移動的像素範圍。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; longPress.delegate = self; longPress.minimumPressDuration = 1.0; longPress.allowableMovement = 30; [self.image addGestureRecognizer:longPress];
五、縮放手勢 UIPinchGestureRecognizer
scale 表示相對於初始尺寸的縮放比例。
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; pinch.delegate = self; pinch.scale = 1.2; [self.image addGestureRecognizer:pinch];
六、旋轉手勢 UIRotationGestureRecognizer
rotation 給出的是弧度值。這裡有個坑:若希望每次都在當前角度基礎上繼續旋轉,必須在回調結束時把該屬性歸零,否則每次都會從初始位置重新計算。
- (void)setupRotation {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.image addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
CGFloat radian = rotation.rotation;
self.image.transform = CGAffineTransformRotate(self.image.transform, radian);
rotation.rotation = 0; // 歸零,保證疊加旋轉
}
七、輕掃手勢 UISwipeGestureRecognizer
輕掃分上下左右四個方向。要注意的是,若同一個檢視上要監聽多個方向,必須為每個方向各建立一個手勢物件,一個物件只能對應一個方向。
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0, // 由左向右
UISwipeGestureRecognizerDirectionLeft = 1 << 1, // 由右向左
UISwipeGestureRecognizerDirectionUp = 1 << 2, // 由下向上
UISwipeGestureRecognizerDirectionDown = 1 << 3 // 由上向下
};
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; swipeUp.delegate = self; swipeUp.direction = UISwipeGestureRecognizerDirectionUp; [self.image addGestureRecognizer:swipeUp]; UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; swipeDown.direction = UISwipeGestureRecognizerDirectionDown; [self.image addGestureRecognizer:swipeDown];
八、拖拽手勢 UIPanGestureRecognizer
這裡最容易搞混的是兩個座標方法:locationInView: 取得的是手指當前在該檢視中的座標;translationInView: 取得的是相對於起始點的累積位移量。
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; pan.delegate = self; [self.image addGestureRecognizer:pan];
// CGPoint p = [pan locationInView:self.image]; // 觸摸點 CGPoint transP = [pan translationInView:self.image]; // 位移點 NSLog(@"%f, %f", transP.x, transP.y); self.image.transform = CGAffineTransformTranslate(self.image.transform, transP.x, transP.y); [pan setTranslation:CGPointZero inView:self.image]; // 位移歸零
實作拖拽時記得把位移量重置為零,這樣每次拿到的才是「本次增量」,而不是從起點累積的總位移,這是新手最常踩的坑。