swift - 如何在快速单击时显示工具提示

标签 swift graphics tooltip line point

我正在使用以下代码绘制线条。线条边缘有点,我想在用户单击末端点时显示工具提示。 代码片段如下,

 override func drawRect(rect: CGRect) {
        // Drawing code
      UIColor.brownColor().set()
    let context = UIGraphicsGetCurrentContext()
    //CGContextSetLineWidth(context, 5)
    CGContextMoveToPoint(context, 50, 50)
    CGContextAddLineToPoint(context,100, 200)
    CGContextStrokePath(context)

    // now add the circle on at the line edges

        var point = CGPoint(x:50 , y:50)
        point.x -= 5.0/2
        point.y -= 5.0/2
        var circle = UIBezierPath(ovalInRect: CGRect(origin: point, size: CGSize(width: 5.0,height: 5.0)))
        circle.fill()

        point = CGPoint(x:100 , y:200)
        point.x -= 5.0/2
        point.y -= 5.0/2
        circle = UIBezierPath(ovalInRect: CGRect(origin: point, size: CGSize(width: 5.0,height: 5.0)))
        circle.fill()

    }

当前显示以下图像, enter image description here

我想显示如下所示的工具提示, enter image description here

有谁知道我如何识别这个特定的点被单击以及如何显示工具提示。 我正在快速寻找解决方案。

最佳答案

定义一个结构来保存您必须触摸的点以及要显示的文本:

struct TouchPoint {
    var point: CGPoint // touch near here to show a tooltip
    var tip: String // show this text when touched
}

然后在定义 drawRectUIView 子类中,创建一个地方来保存它们:

var touchPoints: [TouchPoint] = [] // where we have to touch and what tooltip to show

drawRect 可以调用多次,因此每次都重新开始:

override func drawRect(rect: CGRect) {
    touchPoints = []
    // ...
    // add a touchPoint for every place to touch
    touchPoints.append(TouchPoint(point: point, tip: "point 1"))
}

您需要检测 UIView 上的点击,因此通过更改其初始化来添加手势识别器:

required init?(coder aDecoder: NSCoder) {
    // standard init for a UIView wiht an added gesture recognizer
    super.init(coder: aDecoder)
    addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("touched:")))
}

那么您需要一种方法来查看触摸是否靠近触摸点,并显示正确的工具提示:

func touched(sender:AnyObject) {
    let tapTolerance = CGFloat(20) // how close to the point to touch to see tooltip
    let tipOffset = CGVector(dx: 10, dy: -10) // tooltip offset from point
    let myTag = 1234 // random number not used elsewhere
    guard let tap:CGPoint = (sender as? UITapGestureRecognizer)?.locationInView(self) else { print("touched: failed to find tap"); return }
    for v in subviews where v.tag == myTag { v.removeFromSuperview() } // remove existing tooltips
    let hitPoints:[TouchPoint] = touchPoints.filter({CGPointDistance($0.point, to: tap) < tapTolerance}) // list of tooltips near to tap
    for h in hitPoints { // for each tooltip to show
        let f = CGRect(origin: h.point+tipOffset, size: CGSize(width: 100, height: 20)) // fixed size label :-(
        let l = UILabel(frame: f)
        l.tag = myTag // just to be able to remove the tooltip later
        l.text = h.tip // draw the text
        addSubview(l) // add the label to the view
    }
}

func CGPointDistanceSquared(from: CGPoint, to: CGPoint) -> CGFloat { return (from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y) }

func CGPointDistance(from: CGPoint, to: CGPoint) -> CGFloat { return sqrt(CGPointDistanceSquared(from, to: to)) }

并且顺便使用了新版本的+运算符对CGPoint执行向量加法:

func +(left: CGPoint, right: CGVector) -> CGPoint { return CGPoint(x: left.x+right.dx, y: left.y+right.dy) }

这对我来说没问题。额外的调整是根据文本字符串计算 UILabel 大小,并移动 UILabel,使其不会从边缘的 UIView 一侧跑出。祝你好运!

关于swift - 如何在快速单击时显示工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35915853/

相关文章:

c++ - 如何获取对话框上列表控件的多行工具提示?

css - 是否可以制作延迟后显示的纯 CSS 工具提示?

ios - 如何将任何基数转换为基数 10?

c++ - 如何使用c++制作图像

ios - xib 内的视频 View 未拉伸(stretch)至 Controller 的宽度

c++ - 如何旋转始终与圆形路径相切的对象?

c# - 为什么使用 CellPainting 方法向 datagridview 单元格添加彩色矩形会使单元格中的文本看起来模糊?

css - 某些 HTML 元素的 z-index 不良行为

ios - 这个闭包怎么写?

swift - SpriteKit 场景在模态呈现 swift 后变形