iOS手勢識別的簡單應用

六種手勢

1.手勢分類
  • UITapGestureRecognizer(敲擊)
  • 設置numberOfTapsRequired點擊的次數
  • 結合代理(BOOL) gestureRecognizer能夠選定可點擊的區域
  • UIPinchGestureRecognizer(捏合,用於縮放)
  • 注意復位問題
  • UIPanGestureRecognizer(拖拽)
  • UISwipeGestureRecognizer(輕掃)
  • UIRotationGestureRecognizer(旋轉)
  • UILongPressGestureRecognizer(長按)
    • 長按的兩次響應問題的解決
2.對每個手勢的注意點及代碼實現進行說明
  • UITapGestureRecognizer(敲擊)git

    #pragma mark - tap敲擊
    /**
     *  1.numberOfTapsRequired設置點擊幾回響應
     *  2.代理與(BOOL) gestureRecognizer結合,可肯定圖片哪一個區域能夠點,哪些不能點
     */
    -(void) setTap{
        //建立點按手勢
        //initWithTarget:誰監聽這個事件呢,通常控制器
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        //delegate
        tap.delegate=self;
        //click counts
        tap.numberOfTapsRequired=2;
        //添加手勢,用的是多態addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer(父類)
        [self.imageView addGestureRecognizer:tap];
    }
    
    -(void) tap:(UITapGestureRecognizer *) tap{
        NSLog(@"%s",__func__);
    }
     

#pragma mark - UIGestureRecognizerDelegate //與tap結合 -(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch )touch{ //獲取當前觸摸點在self.imageView的哪一個位置 CGPoint curP=[touch locationInView:self.imageView]; if (curP.x<self.imageView.bounds.size.width0.5) { return YES; }else{ return NO; } }github

```
  • UIPinchGestureRecognizer(捏合,用於縮放)ui

     

#pragma mark - pinch捏合,用於縮放 -(void) setPinch{ UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)]; pinch.delegate=self; [self.imageView addGestureRecognizer:pinch]; } -(void) pinch:(UIPinchGestureRecognizer *) pinch{ NSLog(@"%s",func); self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); pinch.scale=1; } ```.net

  • UIPanGestureRecognizer(拖拽)代理

    #pragma mark - pan拖拽
    /**
     *  復位是指返回到起始的狀態,再能過相對上一個狀態tansform來解決一些問題
     */
    -(void) setPan{
        UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    
        [self.imageView addGestureRecognizer:pan];
    }
    
    -(void) pan:(UIPanGestureRecognizer *) pan{
        NSLog(@"%s",__func__);
    //    CGPoint curP=[pan locationInView:self.imageView];
        //獲取圖片移動距離
        CGPoint curP=[pan translationInView:self.imageView];
        self.imageView.transform=CGAffineTransformTranslate(self.imageView.transform, curP.x, curP.y);
        //復位
        [pan setTranslation:CGPointZero inView:self.imageView];
    }
  • UISwipeGestureRecognizer(輕掃)code

    #pragma mark - swipe輕掃
    -(void) setSwipe{
        //添加向左滑動
        UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
        swipe.direction=UISwipeGestureRecognizerDirectionLeft;
        [self.imageView addGestureRecognizer:swipe];
        //添加向右滑動
        UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
        swipeDown.direction=UISwipeGestureRecognizerDirectionRight;
        [self.imageView addGestureRecognizer:swipeDown];
    
    }
    -(void) swipe:(UISwipeGestureRecognizer *)swipe{
        NSLog(@"%s",__func__);
    }
  • UIRotationGestureRecognizer(旋轉)orm

    #pragma mark - rotation旋轉
    -(void) setRotation{
        UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
        rotation.delegate=self;
        [self.imageView addGestureRecognizer:rotation];
    }
    -(void) rotation:(UIRotationGestureRecognizer *) rotation{
        NSLog(@"%s",__func__);
        NSLog(@"%f",rotation.rotation);
        self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
        [rotation setRotation:0];
    //    rotation.rotation=0;
    
    }
  • UILongPressGestureRecognizer(長按)事件

    #pragma mark - longPress長按
    -(void) setLongPress{
        UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        [self.imageView addGestureRecognizer:longPress];
    }
    /**
     *  長按的一些注意點,那就是長按時觸發一次,離開時再觸發一次
     *  加個判斷,結束與開始選一個觸發的時間
     *  @param longPress 傳參
     */
    -(void) longPress:(UILongPressGestureRecognizer *) longPress{
        if (longPress.state==UIGestureRecognizerStateBegan) {
            NSLog(@"%s",__func__);
        }
    
    }
  • 當同時多個手勢能夠處理時(代理)圖片

     

/**ip

  • 是否支持多個手勢同時處理
  • @param gestureRecognizer 。。
  • @param otherGestureRecognizer 。。
  • @return return value bool */ -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
     
3.源代碼的地址
相關文章
相關標籤/搜索