我已经为这个问题挣扎了几个小时了。我似乎无法理解使用UITapGestureRecognizer的过程。任何帮助都将不胜感激。
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
let textInView = "This is my text."
textView.text = textInView
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapResponse(_:)))
tapGesture.numberOfTapsRequired = 1
textView.addGestureRecognizer(tapGesture)
func tapResponse(sender: UITapGestureRecognizer) {
var location: CGPoint = sender.location(in: textView)
location.x = textView.textContainerInset.left
location.y = textView.textContainerInset.top
print(location.x)
print(location.y)
}
}发布于 2017-04-21 00:15:22
在函数(tapResponse)中有一个函数(viewDidLoad)。
把它放在viewDidLoad之外。然后按如下方式引用:
override func viewDidLoad() {
super.viewDidLoad() // Remember to always call super.
let tapGesture = UITapGestureRecognizer(
target: self,
action: #selector(ViewController.tapResponse(sender:)) // Referencing.
)
tapGesture.numberOfTapsRequired = 1
textView.addGestureRecognizer(tapGesture)
}
// tapResponse is now outside of viewDidLoad:
func tapResponse(sender: UITapGestureRecognizer) {
var location: CGPoint = sender.location(in: imageView)
location.x = textView.textContainerInset.left
location.y = textView.textContainerInset.top
print(location.x)
print(location.y)
}已完成:

https://stackoverflow.com/questions/43531820
复制相似问题