How to check if a place has been tapped in a specific place

I am trying to check if a short press occurs in a certain region on the screen. I created TapGestureRecognizer, this is the code that I have right now:

func handleTap(tap: UITapGestureRecognizer) { var point = tap.locationInView(self.view) println("tapped") if (tap.state == UIGestureRecognizerState.Ended) { if (CGRectContainsPoint(self.Region.frame, point)) { println("touch") var y = kbHeight - textYoutube.center.y youTextConst.constant += y } } } 

The application recognized the tap, but not the tap in a specific area, what is wrong with the code?

UPDATE

I don't know if it matters, but Region is a TextView

+5
source share
2 answers

( NOTE : I assume that self.Region is a UIView. You are not explaining what this is in your question, so I have to guess what it might be. By the way, please do not use an uppercase letter for the property name. I only do this for so that you easily match your own code.)

Think of coordinate systems. You speak

 var point = tap.locationInView(self.view) 

This means that point is in the self.view coordinate self.view . But this is the wrong coordinate system if you are going to call CGRectContainsPoint. Before you can find the relationship between point and self.Region , you need to get the point in the same coordinate system as self.Region :

 point = self.Region.convertPoint(point, fromView:self.view) 

Now you can use self.Region.bounds , which is in the same coordinate system as the new point value:

 if CGRectContainsPoint(self.Region.bounds, point) 
+7
source

My first answer, even with the sketch, was dirty. I deleted it.

So, here are some ways to achieve what you are looking for. We will assume that your region is a UIView.

Solution 1

You are extracting a point from the main view. , so you need to check if the frame of your regionView supports your point.

 import UIKit class ViewController: UIViewController { @IBOutlet var regionView: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tap = UITapGestureRecognizer() tap.addTarget(self, action: "handleTap:") self.view.addGestureRecognizer(tap) } func handleTap(tap: UITapGestureRecognizer) { if (tap.state == UIGestureRecognizerState.Ended) { println("[handleTap] Tap ended") var point = tap.locationInView(self.view) println("[handleTap] Tap point : x\(point.x) ; y\(point.y) (in self.view)") if (CGRectContainsPoint(self.regionView.frame, point)) { println("[handleTap] Tap is inside regionView") } println() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Decision 2

You extract the point and convert it to the regionView internal coordinate system . Therefore, you need to check if the point is within the borders of your region.

 import UIKit class ViewController: UIViewController { @IBOutlet var regionView: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tap = UITapGestureRecognizer() tap.addTarget(self, action: "handleTap:") self.view.addGestureRecognizer(tap) } func handleTap(tap: UITapGestureRecognizer) { if (tap.state == UIGestureRecognizerState.Ended) { println("[handleTap] Tap ended") var point = tap.locationInView(self.view) println("[handleTap] Tap point : x\(point.x) ; y\(point.y) (in self.view)") point = self.regionView.convertPoint(point, fromView: self.view) println("[handleTap] Tap point converted : x\(point.x) ; y\(point.y) (in self.regionView)") if (CGRectContainsPoint(self.regionView.bounds, point)) { println("[handleTap] Tap is inside regionView") } println() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Decision 3

You get a point from your regionView directly. This way you do not need to convert , but you still need to check if it is within the limits like solution 2.

 import UIKit class ViewController: UIViewController { @IBOutlet var regionView: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let tap = UITapGestureRecognizer() tap.addTarget(self, action: "handleTap:") self.view.addGestureRecognizer(tap) } func handleTap(tap: UITapGestureRecognizer) { if (tap.state == UIGestureRecognizerState.Ended) { println("[handleTap] Tap ended") var point = tap.locationInView(self.regionView) println("[handleTap] Tap point : x\(point.x) ; y\(point.y) (in self.view)") if (CGRectContainsPoint(self.regionView.bounds, point)) { println("[handleTap] Tap is inside regionView") } println() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Alternatively, you can set your gesture recognizer in regionView: self.regionView.addGestureRecognizer(tap) .

Let me know if this helped!

+1
source

Source: https://habr.com/ru/post/1214843/


All Articles