ios 如何以编程方式快速调用 UIView 上的手势点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28675209/
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-31 04:56:46  来源:igfitidea点击:

How to call gesture tap on UIView programmatically in swift

iosswiftiphoneios8uigesturerecognizer

提问by George

I have a UIView and and I have added tap gesture to it:

我有一个 UIView 并且我已经为它添加了点击手势:

let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)

I am trying to call it programmatically in the testfile.

我试图在测试文件中以编程方式调用它。

sendActionForEvent

I am using this function, but it is not working:

我正在使用此功能,但它不起作用:

myView.sendActionForEvent(UIEvents.touchUpDown)

It shows unrecognised selector sent to instance.

它显示发送到实例的无法识别的选择器。

How can I solve this problem?

我怎么解决这个问题?

回答by Salavat Khanov

You need to initialize UITapGestureRecognizerwith a target and action, like so:

您需要UITapGestureRecognizer使用目标和操作进行初始化,如下所示:

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)

Then, you should implement the handler, which will be called each time when a tap event occurs:

然后,您应该实现处理程序,每次发生点击事件时都会调用该处理程序:

@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
    // handling code
}

So now calling your tap gesture recognizer event handler is as easy as calling a method:

所以现在调用你的点击手势识别器事件处理程序就像调用一个方法一样简单:

handleTap()

回答by John Lim

For anyone who is looking for Swift 3solution

对于任何正在寻找Swift 3解决方案的人

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))

view.addGestureRecognizer(tap)

view.isUserInteractionEnabled = true

self.view.addSubview(view)

// function which is triggered when handleTap is called
 func handleTap(_ sender: UITapGestureRecognizer) {
     print("Hello World")
  }

回答by RNHTTR

For Swift 4:

对于Swift 4

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))

view.addGestureRecognizer(tap)

view.isUserInteractionEnabled = true

self.view.addSubview(view)

// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("Hello World")
}

In Swift 4, you need to explicitly indicate that the triggered function is callable from Objective-C, so you need to add @objc too your handleTap function.

在 Swift 4 中,您需要明确指出被触发的函数可从 Objective-C 调用,因此您需要在 handleTap 函数中添加 @objc。

See @Ali Beadle 's answer here: Swift 4 add gesture: override vs @objc

请在此处查看@Ali Beadle 的回答:Swift 4 添加手势:覆盖 vs @objc

回答by eclewlow

Just a note - Don't forget to enabled interaction on the view:

请注意 - 不要忘记在视图上启用交互:

let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))

view.addGestureRecognizer(tap)

// view.userInteractionEnabled = true

self.view.addSubview(view)

回答by Kirit Modi

STEP : 1

第1步

@IBOutlet var viewTap: UIView!

STEP : 2

第2步

var tapGesture = UITapGestureRecognizer()

STEP : 3

步骤 : 3

override func viewDidLoad() {
    super.viewDidLoad()
    // TAP Gesture
    tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1
    viewTap.addGestureRecognizer(tapGesture)
    viewTap.isUserInteractionEnabled = true
}

STEP : 4

第四步

func myviewTapped(_ sender: UITapGestureRecognizer) {

    if self.viewTap.backgroundColor == UIColor.yellow {
        self.viewTap.backgroundColor = UIColor.green
    }else{
        self.viewTap.backgroundColor = UIColor.yellow
    }
}

OUTPUT

输出

enter image description here

在此处输入图片说明

回答by Museer Ahamad Ansari

Implementing tap gesture

实现点击手势

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "touchHappen") 
view.userInteractionEnabled = true
view.addGestureRecognizer(tap)

Calls this function when the tap is recognized.

当点击被识别时调用此函数。

func touchHappen() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    self.view.endEditing(true)
}

Update for For Swift 3

Swift 3 的更新

let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchHappen(_:)))
view.addGestureRecognizer(tap)
view.userInteractionEnabled = true

func touchHappen(_ sender: UITapGestureRecognizer) {
    print("Hello Museer")
}

回答by Giang

Swift 4

斯威夫特 4

let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchTapped(_:)))
    self.view.addGestureRecognizer(tap)

@objc func touchTapped(_ sender: UITapGestureRecognizer) {
}

回答by Neen

This is how it works in Swift 3:

这是它在 Swift 3 中的工作方式:

@IBOutlet var myView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action:#selector(handleTap))

    myView.addGestureRecognizer(tap)
}

func handleTap() {
    print("tapped")
}

回答by George

Complete answer for Swift 4

Swift 4 的完整答案

Step 1: create an outlet for the view

第 1 步:为视图创建一个出口

@IBOutlet weak var rightViewOutlet: UIView!

Step 2: define a tap gesture

第 2 步:定义点击手势

var tapGesture = UITapGestureRecognizer()

Step 3: create ObjC function (called when view tapped)

第 3 步:创建 ObjC 函数(点击视图时调用)

@objc func rightViewTapped(_ recognizer: UIGestureRecognizer) {
    print("Right button is tapped")
}

Step 4: add the following within viewDidLoad()

第 4 步:在 viewDidLoad() 中添加以下内容

let rightTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.rightViewTapped(_:)))
    rightViewOutlet.addGestureRecognizer(rightTap)

回答by ikbal

Swift 5.1Example for three view

Swift 5.1三视图示例

Step:1-> Add storyboard view and add outlet viewController UIView

步骤:1-> 添加故事板视图并添加插座 viewController UIView

@IBOutlet var firstView: UIView!
@IBOutlet var secondView: UIView!
@IBOutlet var thirdView: UIView!

Step:2-> Add storyBoard view Tag

步骤:2-> 添加 storyBoard 视图标签

firstViewsecondViewthirdView

第一视图secondViewthirdView

Step:3-> Add gesture

步骤:3-> 添加手势

override func viewDidLoad() {
        super.viewDidLoad()

        firstView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
        firstView.isUserInteractionEnabled = true
        secondView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
        secondView.isUserInteractionEnabled = true
        thirdView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
        thirdView.isUserInteractionEnabled = true
    }

Step:4-> select view

步骤:4-> 选择视图

@objc func tap(_ gestureRecognizer: UITapGestureRecognizer) {
        let tag = gestureRecognizer.view?.tag
        switch tag! {
        case 1 :
            print("select first view")
        case 2 :
            print("select second view")
        case 3 :
            print("select third view")
        default:
            print("default")
        }
    }