iOS手勢UIGestureRecognizer – iPhone手機開發技術文章 iPhone軟體開發教學課程

UIGestureRecognizer是一個抽象類,定義瞭所有手勢的基本行為,它有6個子類處理具體的手勢:

1.UITapGestureRecognizer (任意手指任意次數的點擊)

// 點擊次數

numberOfTapsRequired

// 手指個數

numberOfTouchesRequired

 

 

 

[plain] 

  1. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
  2. [tapGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
  3. [tapGestureRecognizer setNumberOfTapsRequired:2];
  4. [tapGestureRecognizer setNumberOfTouchesRequired:2];
  5. [self.view addGestureRecognizer:tapGestureRecognizer];
  6. [tapGestureRecognizer release];

     

     

    2.UIPinchGestureRecognizer (兩個手指捏合動作)

     

    // 手指捏合,大於1表示兩個手指之間的距離變大,小於1表示兩個手指之間的距離變小

    scale

    // 手指捏合動作時的速率(加速度)

    velocity

     

     

    [plain]

    1. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
    2. [pinchGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
    3. [self.view addGestureRecognizer:pinchGestureRecognizer];
    4. [pinchGestureRecognizer release];

       

      3.UIPanGestureRecognizer (搖動或者拖拽)

       

       

      // 最少手指個數

      minimumNumberOfTouches

      // 最多手指個數

      maximumNumberOfTouches

       

      [plain]

      1. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
      2. [panGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
      3. [panGestureRecognizer setMinimumNumberOfTouches:1];
      4. [panGestureRecognizer setMaximumNumberOfTouches:5];
      5. [self.view addGestureRecognizer:panGestureRecognizer];
      6. [panGestureRecognizer release];

         

        4.UISwipeGestureRecognizer (手指在屏幕上滑動操作手勢)

         

        // 滑動手指的個數

        numberOfTouchesRequired

        // 手指滑動的方向 (Up,Down,Left,Right)

        direction

         

        [plain]

        1. UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
        2. [swipeGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
        3. [swipeGestureRecognizer setNumberOfTouchesRequired:2];
        4. [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
        5. [self.view addGestureRecognizer:swipeGestureRecognizer];
        6. [swipeGestureRecognizer release];

          5.UIRotationGestureRecognizer (手指在屏幕上旋轉操作)

           

          // 旋轉方向,小於0為逆時針旋轉手勢,大於0為順時針手勢

          rotation

          // 旋轉速率

          velocity

           

           

          [plain]

          1. UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];
          2. [rotationGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
          3. [self.view addGestureRecognizer:rotationGestureRecognizer];
          4. [rotationGestureRecognizer release];

             

            6.UILongPressGestureRecognizer (長按手勢)

             

             

            // Default is 0. The number of full taps required before the press for gesture to be recognized

            numberOfTapsRequired

            // 需要長按的手指的個數

            numberOfTouchesRequired

            // 需要長按的時間,最小為0.5s

            minimumPressDuration

            // 手指按住允許移動的距離

            allowableMovement

             

            [plain]

            1. UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
            2. [longPressGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
            3. [longPressGestureRecognizer setMinimumPressDuration:1.0f];
            4. [longPressGestureRecognizer setAllowableMovement:50.0];
            5. [self.view addGestureRecognizer:longPressGestureRecognizer];
            6. [longPressGestureRecognizer release];

You May Also Like