【问题标题】:Pass UITextView tag as a parameters in UITapGestureRecognizer in swift 5在swift 5中将UITextView标签作为参数传递给UITapGestureRecognizer
【发布时间】:2020-11-08 07:52:04
【问题描述】:

swift 5中的UITapGestureRecognizer#Selector方法有没有办法传递UITextView Tag

如下代码

如果是 -> 怎么做?
如果没有 -> 为什么?

let txtView1Tap = UITapGestureRecognizer(target: self, action: #selector(textViewTapped(_:),tag: txtView1.tag))
let txtView2Tap = UITapGestureRecognizer(target: self, action: #selector(textViewTapped(_:), tag: txtView2.tag))

txtView1.addGestureRecognizer(txtView1Tap)
txtView2.addGestureRecognizer(txtView1Tap)

【问题讨论】:

    标签: swift uilabel swift4 swift5 uitapgesturerecognizer


    【解决方案1】:

    不,因为没有 API 可以做到这一点。

    但是您可以使用 view 属性从 UITapGestureRecognizer 获取文本视图

    @obj func textViewTapped(_ sender : UITapGestureRecognizer) {
        let textView = sender.view as! UITextView
        if textView.tag == ...
    }
    

    【讨论】:

    • 我会切换 textView 并检查 case txtView1: ... 而不是使用标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2022-01-22
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多