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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11355671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:09:53  来源:igfitidea点击:

How do I implement the UITapGestureRecognizer into my application

iosobjective-cuser-interfaceuigesturerecognizeruitapgesturerecognizer

提问by objective_c_pro

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 UITapGestureRecognizerinto my code.

我对编程和 Objective C 很陌生。我想知道如何制作一个应用程序,它有一个空白屏幕和一个持续一分钟的计时器。你应该尽可能快地点击,尽可能长时间地点击。我想知道如何在UITapGestureRecognizer我的代码中实现。

回答by Mathew Varghese

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

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

  1. Conform your class to UIGestureRecognizerDelegateprotocol.

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

    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:

    - (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.

  3. Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and 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:

    [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.

  1. 使您的课程符合UIGestureRecognizerDelegate协议。

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

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

    在这里,动作是将处理手势的选择器。在这里,我们的选择器 handleTapFrom 看起来像:

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

    选择器的参数是手势识别器。我们可以使用这个手势识别器来访问它的属性,例如,我们可以发现手势识别的,喜欢的状态UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded等等。

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

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

    [self.imageView addGestureRecognizer:tapGestureRecognizer];
    
  5. 将手势识别器添加到视图后,设置手势识别器的委托,即将处理所有手势识别器内容的类。在我们的示例代码中,它类似于:

    tapGestureRecognizer.delegate = self;
    

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

回答by objective_c_pro

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

回答by King-Wizard

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

}

回答by dbbudd

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

回答by Matsumoto Kazuya

Works in Xcode 11.4, Swift 5

适用于 Xcode 11.4、Swift 5

class ViewController: UIViewController {

    @IBOutlet weak var targetView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchCallback(_:)))
        self.targetView.addGestureRecognizer(tap)
    }

    @objc func touchCallback(_ sender: UITapGestureRecognizer? = nil) {
        if(sender?.state == .ended) {
            print("tapped")
        }
    }
}

// If you want to touch start, available touchesBegan
class ViewController: UIViewController {

    @IBOutlet weak var targetView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          for touch in touches {
              let location = touch.location(in: targetView)
              print(location)
          }
      }
}