如何检查特定地点是否发生了敲击

时间:2015-03-06 16:18:54

标签: ios swift xcode6 uitapgesturerecognizer

我正在尝试检查屏幕上特定区域是否发生了点按。我已经设置了TapGestureRecognizer,这是我现在的代码:

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

该应用识别了点按,但未识别特定区域的点击,代码有什么问题?

更新

我不知道这是否重要,但Region是TextView

2 个答案:

答案 0 :(得分:11)

注意:我假设self.Region是一个UIView。你没有解释你的问题是什么,所以我必须猜测它可能是什么。顺便说一句。 ,请不要使用大写字母作为财产的名称。我这样做只是为了让您轻松匹配您自己的代码。)

考虑坐标系。你在说

var point = tap.locationInView(self.view)

这意味着point位于self.view的坐标系中。但是,如果您打算调用CGRectContainsPoint,那就是错误的坐标系统。在您确定pointself.Region之间的关系之前,您需要将point置于与self.Region相同的坐标系中:

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

现在您可以使用self.Region.bounds,它与point的新值位于同一坐标系中:

if CGRectContainsPoint(self.Region.bounds, point)

答案 1 :(得分:2)

我的第一个答案,即使是草图,也很混乱。我删除了它。

所以这里有多种方法可以实现您的目标。我们假设您所在的地区是UIView。

解决方案1 ​​

您正在从主视图中检索点,因此您需要检查您的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)")

            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 获得积分。所以你不需要转换它,但你仍然需要检查它是否在界限之内,如解决方案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)

让我知道它是否有帮助!