手勢并發(fā)執(zhí)行的方法
1. 設(shè)置手勢的delegate;實(shí)現(xiàn)代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
手勢的優(yōu)先級設(shè)定
當(dāng)識別為gesture01時(shí)不會立刻觸發(fā)gesture01,而是檢測gesture02是否成功履澳,如果gesture02檢測失敗后再觸發(fā)gesture01,達(dá)到解決沖突的結(jié)果哑子。
[gesture01 requireGestureRecognizerToFail:gesture02];
點(diǎn)按手勢 UITapGestureRecognizer
1. 創(chuàng)建手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
2. 設(shè)置手勢屬性(可省略)
tap.numberOfTapsRequired = 1; 需要點(diǎn)擊的次數(shù),默認(rèn)是1.
tap.numberOfTouchesRequired = 1; 需要點(diǎn)擊的手指數(shù),默認(rèn)是1.
[tap requireGestureRecoginzerToFail:doubleTap]; doubleTap是自定義的一個(gè)雙擊事件.使用這個(gè)方法可以取消掉單擊和雙擊事件并存的情況.
3. 添加手勢
[self.imgView addGestureRecognizer:tap];
4. 實(shí)現(xiàn)方法
- (void)tap:(UITapGestureRecoginzer *)tapGestureRecognizer{
// 點(diǎn)擊后執(zhí)行的方法
}
長按手勢 UILongPressGestureRecognizer
1. 創(chuàng)建手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
2. 設(shè)置屬性
longPress.minimumPressDuration = 0.5; 至少0.5秒后觸發(fā)
3. 添加手勢
[self.imgView addGestureRecognizer:longPress];
4. 實(shí)現(xiàn)方法 與狀態(tài)配合實(shí)現(xiàn)
- (void)longPress:(UILongPressGestureRecognizer *)longPressGesture {
if(longPressGesture.state == UIGestureRecognizerStateBegan) {
// 長按手勢開始
}
if(longPressGesture.state == UIGestureRecognizerStateChanged) {
// 長按手勢 手指位置變化
}
if(longPressGesture.state == UIGestureRecognizerStateFailed) {
// 手勢識別失敗,恢復(fù)默認(rèn)狀態(tài)
}
if(longPressGesture.state == UIGestureRecognizerStateCancelled) {
// 手勢取消乒疏,恢復(fù)默認(rèn)狀態(tài)
}
if(longPressGesture.state == UIGestureRecognizerStateRecognized) {
// 手勢狀態(tài)已響應(yīng)
}
if(longPressGesture.state == UIGestureRecognizerStateEnded) {
// 長按手勢 狀態(tài)結(jié)束
}
}
輕掃手勢盖高,滑動(dòng) UISwipeGestureRecognizer
創(chuàng)建手勢,滑動(dòng)手勢有四個(gè)方向听想,如果處理方法相同則可以使用“按位或 | ”設(shè)置滑動(dòng)方向,若處理方法不同則需要?jiǎng)?chuàng)建多個(gè)對象添加
UISwipeGestureRecognizer *swipe01 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe01.direction = UISwipeGestureRecognizerDirectionDown;
設(shè)置手勢的優(yōu)先級用來解決沖突
[swipe requireGestureRecoginzerToFail:pan];
[self.imgView addGestureRecognizer: swipe01];
UISwipeGestureRecognizer *swipe02 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe02.direction = UISwipeGestureRecognizerDirectionUp;
[self.imgView addGestureRecognizer: swipe02];
UISwipeGestureRecognizer *swipe03 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe03.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[self.imgView addGestureRecognizer: swipe03];
實(shí)現(xiàn)方法
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
- (void)swipe:(UISwipeGestureRecognizer *)swipeGesture {
// 判斷方向?qū)崿F(xiàn)不同方法 向左向右不能執(zhí)行因?yàn)闋顟B(tài)為:3;
if(swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
// 向左滑動(dòng)
}
if(swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
// 向右滑動(dòng)
}
if(swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
// 向上滑動(dòng)
}
if(swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
// 向下滑動(dòng)
}
}
拖動(dòng)手勢 UIPanGestureRecognizer
1. 創(chuàng)建手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
2. 添加手勢
[self.imgView addGestureRecognizer:pan];
3. 實(shí)現(xiàn)方法
- (void)pan:(UIPanGestureRecognizer *)pan {
1. localInView 是觸摸點(diǎn)在View的位置
2. translationInView 是點(diǎn)位移的位置
3. 手勢.view 是手勢添加在View身上
CGPoint translation = [pan translationInView:pan.view];
CGPoint center = pan.view.center;
center.x = center.x + translation.x;
center.y = center.y + translation.y;
pan.view.center = center;
// 復(fù)位
[pan setTranslation:CGPointZero inView:pan.view];
}
旋轉(zhuǎn)手勢 UIRotationGestureRecognizer
1. 創(chuàng)建手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
2. 添加手勢
[self.imgView addGestureRecognizer:rotation];
3. 實(shí)現(xiàn)方法
- (void)rotation:(UIRotationGestureRecognizer *)rotationGesture {
//獲取旋轉(zhuǎn)的角度
CGFloat scale = rotationGesture.rotation;
// 設(shè)置view的角度,使用transform設(shè)置
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, scale);
// 復(fù)位
rotationGesture.rotation = 0;
}
捏合手勢 縮放 UIPinchGestureRecognizer
1. 創(chuàng)建手勢
UIPinchGestureRecognizer *pinch = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
2. 添加手勢
[self.imgView addGestureRecognizer:pinch];
3. 實(shí)現(xiàn)方法
- (void)pinch:(UIPinchGestureRecognizer *)pinchGesture {
//獲取縮放值
CGFloat scale = pinchGesture.scale;
// 設(shè)置值
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, scale, scale);
// 復(fù)位
pinchGesture.scale = 1;
}
屏幕邊緣滑動(dòng)手勢 UIScreenEdgePanGestureRecognizer
screenEdgePan是UIPanGestureRecognizer的子類贱鼻,用法相同宴卖,不過screenEdgePan是專用于響應(yīng)從屏幕邊緣滑動(dòng)的效果。
可設(shè)置四個(gè)方向的效果.
screenEdgePan.edges = UIRectEdgeAll;
screenEdgePan.edges = UIRectEdgeLeft | UIRectEdgeRight;