2017-04-21 7 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

감사합니다! 그것은 저를 오랫동안 괴롭 혔습니다. –