1.手勢識別概述
- 如果想監聽一個view上面的觸摸事件,之前的做法是
- 自定義一個view
- 實現view的touches方法,在方法內部實現具體處理代碼
- 但是通過touches方法監聽view觸摸事件,有很明顯的幾個缺點
- 必須得自定義view
- 由于是在view內部的touches方法中監聽觸摸事件,因此默認情況下,無法讓其他外界對象監聽view的觸摸事件
- 不容易區分用戶的具體手勢行為
- iOS 3.2之后,蘋果推出了手勢識別功能(Gesture Recognizer),在觸摸事件處理方面,大大簡化了開發者的開發難度
2.UIGestureRecognizer(手勢識別器)
- 為了完成手勢識別,必須借助于手勢識別器—-UIGestureRecognizer
- 利用UIGestureRecognizer,能輕松識別用戶在某個view上面做的一些常見手勢
- UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢
- UITapGestureRecognizer(敲擊)
- UIPinchGestureRecognizer(捏合,用于縮放)
- UIPanGestureRecognizer(拖拽)
- UISwipeGestureRecognizer(輕掃)
- UIRotationGestureRecognizer(旋轉)
- UILongPressGestureRecognizer(長按)
3.手勢識別器的基本使用
- UITapGestureRecognizer(點擊手勢)
#pragma mark - 添加點擊手勢
-(
void)setUpTap{UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:
self action:
@selector(tap:)];
tap
.delegate =
self;[
self.redView addGestureRecognizer:tap];
}
#pragma mark -UIGestureRecognizerDelegate
-(
BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint curP = [touch locationInView:
self.redView];
if (curP
.x >
self.redView.bounds.size.width *
0.5) {
return NO;}
else{
return YES;}
}
-(
void)tap:(UITapGestureRecognizer *)tap{
NSLog(@
"敲擊手勢");
}
- UIPinchGestureRecognizer(捏合手勢)
#pragma mark - 添加捏合手勢
-(
void)setUpPin{UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:
self action:
@selector(pin:)];[
self.redView addGestureRecognizer:pin];
}
-(
void)pin:(UIPinchGestureRecognizer *)pin{
self.redView.transform = CGAffineTransformScale(
self.redView.transform,pin
.scale, pin
.scale);pin
.scale =
1.0;
}
- UISwipeGestureRecognizer(輕掃手勢)
#pragma mark - 添加左右輕掃手勢
-(
void)setUpSwipe{UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:
self action:
@selector(swipe:)];swipe
.direction = UISwipeGestureRecognizerDirectionLeft;[
self.redView addGestureRecognizer:swipe];UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:
self action:
@selector(swipe:)];swipe1
.direction = UISwipeGestureRecognizerDirectionRight;[
self.redView addGestureRecognizer:swipe1];
}
-(
void)swipe:(UISwipeGestureRecognizer *)swipe{
if(swipe
.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@
"向左掃");}
else if(swipe
.direction == UISwipeGestureRecognizerDirectionRight){
NSLog(@
"向右掃");}
}
#pragma mark - 添加旋轉+捏合手勢
-(
void)setUpRotation{UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:
self action:
@selector(rotation:)];rotation
.delegate =
self;[
self.redView addGestureRecognizer:rotation];UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:
self action:
@selector(pin:)];pin
.delegate =
self;[
self.redView addGestureRecognizer:pin];}
-(
void)rotation:(UIRotationGestureRecognizer *)rotation{
self.redView.transform = CGAffineTransformRotate(
self.redView.transform, rotation
.rotation);rotation
.rotation =
0;
}
-(
void)pin:(UIPinchGestureRecognizer *)pin{
self.redView.transform = CGAffineTransformScale(
self.redView.transform,pin
.scale, pin
.scale);pin
.scale =
1.0;
}
#pragma mark -UIGestureRecognizerDelegate
-(
BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
#pragma mark -UIGestureRecognizerDelegate
-(
BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- UILongPressGestureRecognizer(長按手勢)
#pragma mark - 添加長安手勢
-(
void)setUpPress{UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:
self action:
@selector(longPress:)];longPress
.minimumPressDuration =
1.0;longPress
.allowableMovement =
5;[
self.redView addGestureRecognizer:longPress];
}
-(
void)longPress:(UILongPressGestureRecognizer *)longPress{
NSLog(@
"長按手勢");
}
- UIPanGestureRecognizer(拖拽手勢)
#pragma mark - 添加拖拽手勢
-(
void)setUpPan{UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:
self action:
@selector(pan:)];[
self.redView addGestureRecognizer:pan];
}
-(
void)pan:(UIPanGestureRecognizer *)pan{
CGPoint curP = [pan translationInView:
self.redView];
self.redView.transform = CGAffineTransformTranslate(
self.redView.transform, curP
.x, curP
.y);[pan setTranslation:CGPointZero inView:
self.redView];
}
總結
以上是生活随笔為你收集整理的iOS开发概述-12.手势处理的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。