IOS開發之——事件處理-手勢識別

文章搬運來源:blog.csdn.net/Calvin_zhou…面試

做者:PGzxcmarkdown

對iOS開發感興趣,能夠看一下做者的iOS交流羣:812157648,你們能夠在裏面吹水、交流相關方面的知識,羣裏還有我整理的有關於面試的一些資料,歡迎你們加羣,你們一塊兒開車oop

一 概述

  • 監聽觸控事件的作法
  • 手勢識別器——UIGestureRecognizer
  • 手勢識別示例

二 監聽觸摸事件的作法

2.1 若是想監聽一個view上面的觸摸事件,以前的作法是

  • 自定義一個view
  • 實現view的touches方法,在方法內部實現具體的處理代碼

2.2 經過touches方法監聽view觸摸事件,有很明顯的幾個缺點

  • 必須得自定義view
  • 因爲是在view內部的touches方法中監聽觸摸事件,所以默認狀況下,沒法讓其餘外界對象監聽view的觸摸事件
  • 不容易區分用戶的具體手勢行爲

2.3 手勢識別功能

IOS3.2以後,蘋果推出了手勢識別功能(Guesture Recognizer),在觸摸事件處理方面,大大簡化了開發者的開發難度ui

三 手勢識別器——UIGestureRecognizer

3.1手勢識別器介紹

  • 爲了完成手勢識別,必須藉助手勢識別器——UIGestureRecognizer
  • 利用UIGestureRecognizer,能輕鬆識別用戶在某個view上面作的一些常見手勢

3.2 手勢識別器的常見行爲

UIGestureRecognizer是一個抽象類,定義了全部手勢的基本行爲,使用它的子類才能處理具體的手勢spa

  • UITapGestureRecognizer:敲擊
  • UIPinchGestureRecognizer:捏合,用於縮放
  • UIPanGestureRecognizer:拖拽
  • UISwipeGestureRecognizer:輕掃
  • UIRotationGestureRecognizer:旋轉
  • UILongPressGestureRecognizer:長按

四 手勢識別示例

4.1 UITapGestureRecognizer

手勢

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

  //點按多少次才能觸發
  tap.numberOfTapsRequired=2;
  //必須多少個手指觸摸才能觸發手勢
  //tap.numberOfTouchesRequired=2;
  tap.delegate=self;
  [_imageView addGestureRecognizer:tap];

複製代碼

說明

  • tap.numberOfTapsRequired:點擊多少次才能觸發
  • tap.numberOfTouchesRequired:多少手勢觸發

方法

-(void)tap:(UITapGestureRecognizer *)tap
{
    NSLog(@"tap");
}

複製代碼

4.2 UILongPressGestureRecognizer

手勢

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
 [_imageView addGestureRecognizer:longPress];

複製代碼

方法

-(void)longPress:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state==UIGestureRecognizerStateBegan) {
        NSLog(@"longPress");
    }
}

複製代碼

4.3 UISwipeGestureRecognizer

手勢

//swip 一個手勢只能識別一個方向
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
swipe.direction=UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:swipe];

複製代碼

方法

-(void)swip:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"swipe");
}

複製代碼

4.4 UIRotationGestureRecognizer

添加手勢

UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
 [_imageView addGestureRecognizer:rotation];

複製代碼

方法

-(void)rotation:(UIRotationGestureRecognizer *)rotation
{
    NSLog(@"%f",rotation.rotation);
    //_imageView.transform=CGAffineTransformMakeRotation(rotation.rotation);
    _imageView.transform=CGAffineTransformRotate(_imageView.transform, rotation.rotation);
    rotation.delegate=self;
    rotation.rotation=0;
}

複製代碼

4.5 UIPinchGestureRecognizer

手勢

UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
 [_imageView addGestureRecognizer:pinch];
 pinch.delegate=self;
 [self addRotation];

複製代碼

方法

-(void)pinch:(UIPinchGestureRecognizer *)pinch
{
    _imageView.transform=CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
    //復位
    pinch.scale=1;
}

複製代碼

4.6 UIPanGestureRecognizer

手勢

UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];

複製代碼

方法

-(void)pan:(UIPanGestureRecognizer *)pan
{
    CGPoint trans=[pan translationInView:_imageView];
    _imageView.transform=CGAffineTransformTranslate(_imageView.transform, trans.x, trans.y);
    //復位
    [pan setTranslation:CGPointZero inView:_imageView];
    NSLog(@"%@",NSStringFromCGPoint(trans));
}
複製代碼
相關文章
相關標籤/搜索