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

时间:2012-07-06 04:05:00

标签: ios objective-c user-interface uigesturerecognizer uitapgesturerecognizer

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

5 个答案:

答案 0 :(得分:93)

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

  1. 使您的班级符合UIGestureRecognizerDelegate协议。

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

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

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

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

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

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

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

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

    tapGestureRecognizer.delegate = self;
    

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

答案 1 :(得分:14)

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

答案 2 :(得分:13)

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

}

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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