iOS開發中六種手勢識別

jopen 9年前發布 | 146K 次閱讀 手勢識別 iOS開發 移動開發

iOS開發中手勢識別有六種:

輕擊手勢(TapGestureRecognizer),

輕掃手勢 (SwipeGestureRecognizer),

長按手勢(LongPressGestureRecognizer),

拖動手勢(PanGestureRecognizer),

捏合手勢(PinchGestureRecognizer),

旋轉手勢(RotationGestureRecognizer),

1,輕擊手勢(TapGestureRecognizer)

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //點擊次數
tapGesture.numberOfTouchesRequired = 1; //點擊手指數
[self.view addGestureRecognizer:tapGesture];

//輕擊手勢觸發方法 -(void)tapGesture:(UITapGestureRecognizer *)sender { //your code }</pre>

2,長按手勢(LongPressGestureRecognizer)

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//設置長按時間
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//長按手勢觸發方法
-(void)longPressGesture:(id)sender
{
     UILongPressGestureRecognizer *longPress = sender;
     if (longPress.state == UIGestureRecognizerStateBegan)
     {
         //your code
     }
}
說明:長按手勢的常用狀態如下
開始:UIGestureRecognizerStateBegan
改變:UIGestureRecognizerStateChanged
結束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失敗:UIGestureRecognizerStateFailed

3,輕掃手勢(SwipeGestureRecognizer)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//設置輕掃的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右
[self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//設置輕掃的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左
[self.view addGestureRecognizer:swipeGestureLeft];
//輕掃手勢觸發方法
-(void)swipeGesture:(id)sender
{
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        //向左輕掃
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        //向右輕掃
    }
}

4,捏合手勢(PinchGestureRecognizer)

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

5,拖動手勢(PanGestureRecognizer)

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];

//拖動手勢觸發方法 -(void) panGesture:(id)sender { UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //your code }</pre>

6,旋轉手勢(RotationGestureRecognizer)

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋轉手勢觸發方法
-(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;//取消形變
        }];
    }
}

更多iOS開發相關技術請關注iOS開發微信公眾號 iOS開發 :

原文 http://www.superqq.com/blog/2015/01/14/ioskai-fa-zhi-shou-shi-shi-bie-hui-zo

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!