2017-04-21 2 views
0

私はこの問題を数時間苦労しています。私はUITapGestureRecognizerを使用する手順を理解できないようです。どんな助けもありがとう。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) 
    } 
} 

答えて

2

あなたは機能(viewDidLoad)内部機能(tapResponse)を持っています。

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) 
} 

が完了:

works

+0

ありがとうございます!それは長い間私を悩ませてきました。 –

関連する問題