我的第一个答案,即使是草图,也很混乱。我已经删除了。
因此,这里有多种方法可以实现您的目标。我们假设您的区域是一个 UIView。
解决方案 1
您正在从 主视图 检索点,因此您需要检查您的 regionView 的 frame 是否包含您的点。
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.
}
}
解决方案 2
您正在检索该点并将其转换到内部 regionView 坐标系。因此,您需要检查该点是否在您的 regionView 的 bounds 内。
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.
}
}
解决方案 3
您可以直接从您的 regionView 中获得要点。所以你不需要转换它,但是你仍然需要检查它是否在它的 bounds 之内,就像解决方案 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.
}
}
或者,您只能在 regionView 上设置手势识别器:self.regionView.addGestureRecognizer(tap)。
如果有帮助请告诉我!