My first answer, even with the sketch, was messy. I've deleted it.
So here are multiple ways to achieve what you're looking for. We'll assume that your region is an UIView.
Solution 1
You are retrieving the point from the main view, so you need to check if the frame of your regionView contains 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.
}
}
Solution 2
You are retrieving the point and convert it to the inside regionView coordinates system. So you need to check if the point is within bounds of your regionView.
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.
}
}
Solution 3
You get the point from your regionView directly. So you don't need to convert it, however you still need to check if it's within its bounds 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 only set your gesture recognizer on the regionView : self.regionView.addGestureRecognizer(tap)
.
Let me know if it helped !