在Swift中获取坐标(x,y)处的UILabel值 [英] Get value of UILabel at coordinate (x,y) in Swift

查看:407
本文介绍了在Swift中获取坐标(x,y)处的UILabel值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来在给定的(x,y)坐标处找到UILabel的值(文本)。

I am looking for a way to find the value(text) of a UILabel at a given (x,y) coordinate.

所有其他问题似乎都与获取标签的[x,y]有关,我正尝试相反的做法...

All other questions seem to be about getting the (x,y) of a label, I am trying to do the opposite...

例如(30,40)将返回该位置处Label的值。

For example (30,40) would return the value of the Label at that location.

谢谢!

更新

func getLabel(location: CGPoint){

  let view = self.view.hitTest(location, withEvent:nil)
  //I would like to get the value of the label here but I'm not sure how to. 

}

@IBAction func tap(sender: UITapGestureRecognizer) {
    var coordinate = sender.locationInView(GraphView?())
    getLabel(coordinate)
}


推荐答案

点击测试

将所需的点传递到顶层视图,它将返回包含该点的视图层次结构中最深的视图。

Pass the point you want to the top-level view, and it will return the deepest view within the view hierarchy that contains that point.

UIView *view = [self.view hitTest:location withEvent:nil];

在Swift中,它看起来像是

In Swift, it would look like

let selectedView = view.hitTest(location, withEvent: nil)

更新:

您需要将标签的 userInteractionEnabled 设置为true标签来记录命中。

You need to set the label's userInteractionEnabled to true for your labels to register the hit.

您需要检查返回的视图是否是标签,然后检查是否有任何文本。

You'll need to check the returned view to see if it is a label, then check to see if it has any text.

if let label = selectedView as? UILabel
{
    if label.text!
    {
        // .. do what you want with the label's text
    }
}

这篇关于在Swift中获取坐标(x,y)处的UILabel值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆