UITextView:addGestureRecognizer并找到textView的标签

时间:2019-03-10 16:24:23

标签: swift drag-and-drop tags uitextview uitapgesturerecognizer

override func viewDidLoad() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(findTag))
    textViews[i].addGestureRecognizer(tap)
}

var x : Int?

@objc func  findTag (textView : UITextView) {
    x = textView.tag
    print(x)
}

// why it does not work?

func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
    print(x)
    var itemA : UIDragItem?
    for textView in textViews {
        let itemProvider = NSItemProvider(object: textView.text as NSString)
        let item = UIDragItem(itemProvider: itemProvider)
        itemA = item
    }
    print("item")
    print(itemA)
    return [itemA!]
}

我只想找出与哪个textview交互,因为拖动项始终返回您看到的最后一项。

1 个答案:

答案 0 :(得分:0)

我找到了带有session.location和hitTest方法的解决方案。

func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
    let point = session.location(in: self.stackView)
    let n = stackView.hitTest(point, with: nil) as! UITextView
    let itemProvider = NSItemProvider(object: n.text as NSString)
    let item = UIDragItem(itemProvider: itemProvider)

    return [item]
}