首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITapGestureRecognizer错误

UITapGestureRecognizer错误
EN

Stack Overflow用户
提问于 2017-04-21 00:04:03
回答 1查看 31关注 0票数 0

我已经为这个问题挣扎了几个小时了。我似乎无法理解使用UITapGestureRecognizer的过程。任何帮助都将不胜感激。

代码语言:javascript
复制
@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)
    }
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-21 00:15:22

在函数(tapResponse)中有一个函数(viewDidLoad)。

把它放在viewDidLoad之外。然后按如下方式引用:

代码语言:javascript
复制
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)
}

已完成:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43531820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档