34

我對編程和Objective C非常陌生。我想知道如何製作一個應用程序,其中有一個空白屏幕和一分鐘的計時器。你的目的是儘可能快地挖掘,只要你可以。我想知道如何在我的代碼中實現UITapGestureRecognizer如何將UITapGestureRecognizer實現到我的應用程序中

+0

幫助自己:https://github.com/nhahtdh/PS3/blob/master/Game/GameBlock.m – nhahtdh 2012-07-06 04:10:18

回答

86

這是如何實現你的類手勢識別一步一步的指導:

  1. 順應你的類UIGestureRecognizerDelegate協議。

  2. 實例化手勢識別器。例如,實例化一個UITapGestureRecognizer,我們會做:

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

    這裏,動作是將處理手勢選擇。在這裏,我們的選擇器handleTapFrom將如下所示:

    - (void) handleTapFrom: (UITapGestureRecognizer *)recognizer 
    { 
        //Code to handle the gesture 
    } 
    

    選擇器的參數是手勢識別器。我們可以使用這個手勢識別器來訪問它的屬性,例如,我們可以發現手勢識別的狀態一樣,UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded

  3. 坐落在實例化的手勢識別所需的性能。例如,對於UITapGestureRecognizer,我們可以設置屬性numberOfTapsRequirednumberOfTouchesRequired

  4. 將手勢識別器添加到要檢測手勢的視圖。在我們的示例代碼(我將分享給大家參考的代碼),我們將手勢識別器添加到的ImageView與下面的代碼行:

    [self.imageView addGestureRecognizer:tapGestureRecognizer]; 
    
  5. 增加手勢識別到視圖後,設置代表手勢識別器,即將處理所有手勢識別器的類。在我們的示例代碼,它會像:

    tapGestureRecognizer.delegate = self; 
    

    注:增加手勢識別到視圖後指定的代表。否則,該操作方法將不會被調用。

+0

對不起,不恰當的格式! – 2012-07-06 04:17:31

+0

非常感謝您的幫助! – 2012-07-06 04:18:07

+2

沒什麼好擔心的。標記下來的格式有一個學習曲線。 – nhahtdh 2012-07-06 04:18:39

11
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; 
[self.tableView addGestureRecognizer:gestureRecognizer]; 
self.tableView.userInteractionEnabled = YES; 
gestureRecognizer.cancelsTouchesInView = NO; // this prevents the gesture recognizers to 'block' touches 
9

實例斯威夫特

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var myUIImageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:") 
     singleTap.numberOfTapsRequired = 1 
     singleTap.numberOfTouchesRequired = 1 
     self.myUIImageView.addGestureRecognizer(singleTap) 
     self.myUIImageView.userInteractionEnabled = true 
    } 

    func myUIImageViewTapped(recognizer: UITapGestureRecognizer) { 
     if(recognizer.state == UIGestureRecognizerState.Ended){ 
      println("myUIImageView has been tapped by the user.") 
     } 
    } 

} 
+2

沒錯,userInteractionEnabled是必不可少的。謝謝! – 2015-04-02 11:55:24

+0

我不明白爲什麼需要檢查「recognizer.state == UIGestureRecognizerState.Ended」。我測試了這一點,它沒有它也能正常工作。我的假設是,你需要它多點擊手勢識別器,但只需一次敲擊,我們是否仍然需要它? – plam4u 2016-07-05 10:16:33

0

如果您正在斯威夫特(iOS9)和SpriteKit,請嘗試以下。

class GameScene: SKScene { 
    override func didMoveToView(view: SKView) { 
     let singleTap = UITapGestureRecognizer(target:self, action:#selector(self.handleSingleTap:)) 
     singleTap.numberOfTouchesRequired = 1 
     singleTap.addTarget(self, action:#selector(self.handleSingleTap)) 
     view.userInteractionEnabled = true 
     view.addGestureRecognizer(singleTap) 
    } 
    //event handler 
    func handleSingleTap(sender:UITapGestureRecognizer){ 
     print("tapped") 
    } 
} 
相關問題