gpt4 book ai didi

swift - UITextView:添加GestureRecognizer并找到textView的标签

转载 作者:行者123 更新时间:2023-11-30 10:48:51 25 4
gpt4 key购买 nike

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 交互,因为拖动项目总是返回您所看到的最后一个项目。

最佳答案

我找到了 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]
}

关于swift - UITextView:添加GestureRecognizer并找到textView的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com