如何在我的应用程序中实现UITapGestureRecognizer

时间:2021-09-23 21:15:17

I am quite new to programming and Objective C. I was wondering how to make an app which has a blank screen and a timer that goes for one minute. You are meant to tap as fast as you can and as long as you can for. I was wondering how to implement the UITapGestureRecognizer into my code.

我对编程和目标C都很陌生。我想知道如何制作一个有空白屏幕和一分钟计时器的应用程序。你的意思是尽可能快地点击,只要你可以。我想知道如何在我的代码中实现UITapGestureRecognizer。

4 个解决方案

#1


88  

This is a step by step guide on how to implement gesture recognizers in your class:

这是如何在您的班级中实现手势识别器的分步指南:

  1. Conform your class to UIGestureRecognizerDelegate protocol.

    使您的类符合UIGestureRecognizerDelegate协议。

  2. Instantiate the gesture recognizer. For example, to instantiate a UITapGestureRecognizer, we will do:

    实例化手势识别器。例如,要实例化UITapGestureRecognizer,我们将执行以下操作:

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

    Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:

    这里,action是处理手势的选择器。在这里,我们的选择器handleTapFrom将如下所示:

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

    The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like, UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, etc.

    选择器的参数是手势识别器。我们可以使用此手势识别器来访问其属性,例如,我们可以找到手势识别器的状态,例如UIGestureRecognizerStateBegan,UIGestureRecognizerStateEnded等。

  3. Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and numberOfTouchesRequired.

    在实例化的手势识别器上设置所需的属性。例如,对于UITapGestureRecognizer,我们可以设置属性numberOfTapsRequired和numberOfTouchesRequired。

  4. Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:

    将手势识别器添加到要检测手势的视图中。在我们的示例代码中(我将分享该代码供您参考),我们将使用以下代码行向imageView添加手势识别器:

    [self.imageView addGestureRecognizer:tapGestureRecognizer];
    
  5. After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:

    在将姿势识别器添加到视图之后,设置姿势识别器的代理,即将处理所有姿势识别器内容的类。在我们的示例代码中,它将是:

    tapGestureRecognizer.delegate = self;
    

    Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.

    注意:将手势识别器添加到视图后分配代理。否则,将不会调用action方法。

#2


12  

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

#3


10  

Example in Swift:

Swift中的示例:

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.")
        }
    }

}

#4


0  

If you are working Swift (iOS9) and SpriteKit, try the following.

如果您正在使用Swift(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")
    }
}

#1


88  

This is a step by step guide on how to implement gesture recognizers in your class:

这是如何在您的班级中实现手势识别器的分步指南:

  1. Conform your class to UIGestureRecognizerDelegate protocol.

    使您的类符合UIGestureRecognizerDelegate协议。

  2. Instantiate the gesture recognizer. For example, to instantiate a UITapGestureRecognizer, we will do:

    实例化手势识别器。例如,要实例化UITapGestureRecognizer,我们将执行以下操作:

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

    Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:

    这里,action是处理手势的选择器。在这里,我们的选择器handleTapFrom将如下所示:

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

    The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like, UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, etc.

    选择器的参数是手势识别器。我们可以使用此手势识别器来访问其属性,例如,我们可以找到手势识别器的状态,例如UIGestureRecognizerStateBegan,UIGestureRecognizerStateEnded等。

  3. Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and numberOfTouchesRequired.

    在实例化的手势识别器上设置所需的属性。例如,对于UITapGestureRecognizer,我们可以设置属性numberOfTapsRequired和numberOfTouchesRequired。

  4. Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:

    将手势识别器添加到要检测手势的视图中。在我们的示例代码中(我将分享该代码供您参考),我们将使用以下代码行向imageView添加手势识别器:

    [self.imageView addGestureRecognizer:tapGestureRecognizer];
    
  5. After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:

    在将姿势识别器添加到视图之后,设置姿势识别器的代理,即将处理所有姿势识别器内容的类。在我们的示例代码中,它将是:

    tapGestureRecognizer.delegate = self;
    

    Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.

    注意:将手势识别器添加到视图后分配代理。否则,将不会调用action方法。

#2


12  

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

#3


10  

Example in Swift:

Swift中的示例:

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.")
        }
    }

}

#4


0  

If you are working Swift (iOS9) and SpriteKit, try the following.

如果您正在使用Swift(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")
    }
}