監(jiān)聽(tīng)觸摸事件的做法:
1.如果想監(jiān)聽(tīng)一個(gè)view上面的觸摸事件,之前的做法是:
1>自定義一個(gè)view
2>實(shí)現(xiàn)view的touches的方法,在方法內(nèi)部實(shí)現(xiàn)具體處理代碼
2.通過(guò)touches方法監(jiān)聽(tīng)view觸摸事件,有很明顯的幾個(gè)缺點(diǎn)
1>必須得自定義view
2>由于是在view內(nèi)部的touches方法中監(jiān)聽(tīng)觸摸事件,默認(rèn)情況下,是無(wú)法讓其他外界對(duì)象監(jiān)聽(tīng)view的觸摸事件
3>不容易區(qū)分用戶的具體手勢(shì)行為
iOS 3.2之后,蘋果退出了手勢(shì)識(shí)別功能(Gesture Recognizer),在觸摸手勢(shì)處理方面,大大簡(jiǎn)化了開(kāi)發(fā)者的開(kāi)發(fā)難度
UIGestureRecognizer---手勢(shì)識(shí)別器
UIGestureRecognizer是一個(gè)抽象類,定義了所有手勢(shì)的基本行為,使用它的子類才能處理具體手勢(shì)行為
1.UITapGestureRecognizer 點(diǎn)擊行為
2.UIPinchGestureRecognizer 捏合行為,用于縮放
3.UIPanGestureRecognizer 拖拽行為
4.UISwipeGestureRecognizer 輕掃行為
5.UIRotationGestureRecognizer 旋轉(zhuǎn)行為
6.UILongPressGestureRecognizer 長(zhǎng)按行為
手勢(shì)識(shí)別器的用法:以UITapGestureRecognizer為例
1.創(chuàng)建手勢(shì)識(shí)別器對(duì)象 (可以直接添加代理,和執(zhí)行的方法)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init] [tap addTarget:self action:]
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector()]
2.設(shè)置手勢(shì)識(shí)別器對(duì)象的具體屬性(一般不設(shè)置)
//連續(xù)敲擊2次
tap.numberOfTapsRequired = 2;
//需要2根手指一起敲擊
tap.numberOfTouchesRequired = 2;
3.添加手勢(shì)識(shí)別器到對(duì)應(yīng)的view上
[self.view addGestureRecognizer: tap]
手勢(shì)識(shí)別的狀態(tài)
typedef NS_ENUM(NSInteger,UIGestureRecognizerState)
{
//沒(méi)有觸摸事件發(fā)生,所有手勢(shì)識(shí)別的默認(rèn)狀態(tài)
UIGestureRecognizerStatePossible,
//一個(gè)手勢(shì)已經(jīng)開(kāi)始但尚未改變或者完成時(shí)
UIGestureRecognizerStateBegan,
//手勢(shì)狀態(tài)改變
UIGestureRecognizerStateChanged,
//手勢(shì)完成
UIGestureRecognizerStateEnded,
//手勢(shì)取消,恢復(fù)至Possible狀態(tài)
UIGestureRecognizerStateCancelled,
//手勢(shì)失敗,恢復(fù)至Possible狀態(tài)
UIGestureRecognizerStateFailed,
}
手勢(shì)的應(yīng)用(點(diǎn)擊手勢(shì))
- (void)viewDidLoad {
[super viewDidLoad];
// 建立點(diǎn)擊手勢(shì)具伍,并制定target ,并要求實(shí)現(xiàn)tap方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
// 將點(diǎn)擊手勢(shì)册舞,加入_imageView的處理中, 記得一定要選擇交互 匹表, 因?yàn)槟J(rèn)情況下 imageView的交互為no
[_imageView addGestureRecognizer:tap];
// 設(shè)置點(diǎn)擊手勢(shì)的代理偶翅,可以做一些特定的事情
tap.delegate = self;
}
// 點(diǎn)擊手勢(shì)的范圍縮減辜腺, 只接受圖片一半的尺寸妄均。
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch
{
CGPoint tapP = [touch locationInView:_imageView];
if (tapP.x < _imageView.frame.size.width * 0.5) {
return YES;
}
return NO;
}
// 實(shí)現(xiàn)點(diǎn)擊手勢(shì),方法
- (void)tap{
NSLog(@"%s",__func__);
}
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch
此方法可以設(shè)置允許手勢(shì)的區(qū)域 ,默認(rèn)為YES.
長(zhǎng)按和輕掃手勢(shì).
#pragma mark - <輕掃手勢(shì)>
- (void)setupSwipe
{
// 多加幾個(gè)輕掃手勢(shì)哪自,便能夠?qū)崿F(xiàn)多個(gè)方向的輕掃。
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
[_imageView addGestureRecognizer:swipe];
// 修改輕掃的的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
[_imageView addGestureRecognizer:swipe1];
// 修改輕掃的的方向
swipe1.direction = UISwipeGestureRecognizerDirectionRight;
}
- (void)swipe
{
NSLog(@"%s",__func__);
}
#pragma mark - <長(zhǎng)按點(diǎn)擊>
- (void)setupLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[_imageView addGestureRecognizer:longPress];
longPress.delegate = self;
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 傳入?yún)?shù)禁熏, 當(dāng)前的手勢(shì)壤巷, 手勢(shì)狀態(tài),決定事件
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"%s",__func__);
}
}
捏合,拖拽手勢(shì)
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
默認(rèn)為NO, 返回YES是允許多重手勢(shì).
// 允許組合手勢(shì)
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - <旋轉(zhuǎn)>
- (void)setupRotation
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[_imageView addGestureRecognizer:rotation];
rotation.delegate = self;
}
- (void)rotation:(UIRotationGestureRecognizer*)rotation
{
CGFloat rotationF = rotation.rotation;
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotationF);
rotation.rotation = 0;
}
#pragma mark - <捏合>
- (void)setupPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[_imageView addGestureRecognizer:pinch];
pinch.delegate = self;
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
// 獲取捏合比例
CGFloat scale = pinch.scale;
_imageView.transform = CGAffineTransformScale(_imageView.transform, scale, scale);
// 捏合后保持原比例瞧毙,負(fù)責(zé)會(huì)變得不見(jiàn)得
pinch.scale = 1;
}
#pragma mark - <拖拽>
- (void)setupPan
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint panP = [pan translationInView:_imageView];
NSLog(@"%@",NSStringFromCGPoint(panP));
// 如果不去讓pan每次的tranlation為0 的話胧华, 則再下次拖拽時(shí) 寄症,圖片會(huì)直接回到初始位置,因?yàn)樯弦淮蔚膒an的translation沒(méi)有清掉矩动。
// _imageView.transform = CGAffineTransformMakeTranslation(panP.x, panP.y);
_imageView.transform = CGAffineTransformTranslate(_imageView.transform, panP.x, panP.y);
// 每次拖拽有巧,translation 設(shè)定為0 ,可以每次根據(jù)上一次的情況定位悲没,去實(shí)現(xiàn)拖拽篮迎。
[pan setTranslation:CGPointZero inView:_imageView];
}