華山論劍之iOS手勢(UIGestureRecognizer )

子所不欲,勿施于人.


iOS系統(tǒng)在3.2版本之后提供了一些常用的手勢,總共是6大手勢,總共是六大類,當然了,當這些手勢不能滿足我們的工程需要的時候,我們需要自定義手勢,這個手勢類繼承于UIGestureRecognizer這個類.

手勢名稱 手勢類
點擊手勢 UITapGestureRecognizer
輕掃手勢 UISwipeGestureRecognizer
長按手勢 UILongPressGestureRecognizer
旋轉(zhuǎn)手勢 UIRotationGestureRecognizer
拖動手勢 UIPanGestureRecognizer
捏合手勢 UIPinchGestureRecognizer


UITapGestureRecognizer (點擊手勢)

點擊手勢比較簡單,但是也是我們最常用的一種手勢,我們只需要創(chuàng)建出手勢并且添加到對應(yīng)的視圖上即可.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];//tapAction:這個方法中我們添加當點擊手勢觸發(fā)的時候,我們需要執(zhí)行的方法

tapGesture.numberOfTapsRequired = 1; 
//點擊次數(shù)

tapGesture.numberOfTouchesRequired = 1; 
//點擊手指數(shù)

[self.view addGestureRecognizer:tapGesture];


UISwipeGestureRecognizer(輕掃手勢)

輕掃手勢當,有左右輕掃之分,所以我們可以區(qū)別對待,向左輕掃和向右輕掃

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];

//設(shè)置輕掃的方向

swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; 
//向右

[self.view addGestureRecognizer:swipeGesture];

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];

//設(shè)置輕掃的方向

swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
//向左

[self.view addGestureRecognizer:swipeGestureLeft];

//輕掃手勢觸發(fā)方法

下面的方法可以進行左右輕掃的設(shè)置.

-(void)swipeGesture:(id)sender
{
UISwipeGestureRecognizer *swipe = sender
if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)   {      
//向左輕掃
   
  } if(swipe.direction == UISwipeGestureRecognizerDirectionRight){     
//向右輕掃    
  }

}


UILongPressGestureRecognizer(長按手勢)

長按手勢也是一種常用的手勢,一般我們會用在圖片上面,長按之后出現(xiàn)選項對話框,這時候,用戶就可以進行進一步的用戶交互了.

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//設(shè)置長按時間
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//長按手勢觸發(fā)方法
-(void)longPressGesture:(id)sender
{
     UILongPressGestureRecognizer *longPress = sender;
     if (longPress.state == UIGestureRecognizerStateBegan)
     {
      
     }
}

長按手勢的常用狀態(tài)如下
開始:UIGestureRecognizerStateBegan
改變:UIGestureRecognizerStateChanged
結(jié)束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失敗:UIGestureRecognizerStateFailed


UIPanGestureRecognizer (拖動手勢)

拖動手勢,也是一種常見的手勢,我們一般用圖標的拖動.

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
//拖動手勢觸發(fā)方法
-(void) panGesture:(id)sender
{
    UIPanGestureRecognizer *panGesture = sender;
    CGPoint movePoint = [panGesture translationInView:self.view];
    //your code
}


UIPinchGestureRecognizer (捏合手勢)

捏合手勢一般用于圖片放大之后的還原

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
    ////捏合手勢觸發(fā)方法
-(void) pinchGesture:(id)sender
{
    UIPinchGestureRecognizer *gesture = sender;
    //手勢改變時
    if (gesture.state == UIGestureRecognizerStateChanged)
    {
         //捏合手勢中scale屬性記錄的縮放比例
        _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }
    //結(jié)束后恢復(fù)
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformIdentity;//取消一切形變
        }];
    }
}


UIRotationGestureRecognizer (旋轉(zhuǎn)手勢)

旋轉(zhuǎn)手勢也是我們常見的一種手勢之一

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋轉(zhuǎn)手勢觸發(fā)方法
-(void)rotationGesture:(id)sender
{
    UIRotationGestureRecognizer *gesture = sender;
    if (gesture.state==UIGestureRecognizerStateChanged)
    {
        _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
    }
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1 animations:^{
            _imageView.transform=CGAffineTransformIdentity;//取消形變
        }];
    }
}


注意:一個手勢只能對應(yīng)一個 View,但是一個 View 可以有多個手勢。

原文參考:http://www.open-open.com/lib/view/open1435284827732.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容