2016-03-10 37 views
0

我使用以下代码绘制线条。线边有点,我想在用户点击结束点时显示工具提示。 的代码片断如下,如何在swift中点击点击工具提示

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

有谁有一个想法如何我会认识到这个特定的点被点击,并且我将如何显示工具提示。 我在swift中寻找解决方案。

回答

1

定义一个struct抱着你必须触摸点,以及文字显示:

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

然后在UIView子类中,可以定义drawRect,使地方,让他们:

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的边缘处运行。祝你好运!

+0

我很感谢你的回答,但我意识到管理标签以及设备方向的变化非常困难,所以我采取了一种稍微不同的方法来完成这项任务。在我绘制圆圈的最终点,我将它们作为按钮,在按钮上有一个点图像。然后在按钮上单击我显示缩小大小popover并显示工具提示。它对我来说是完美的,但我很欣赏你的努力。 – neena

+0

我发现这是一个有趣的问题,我扩展了这段代码以便像弹出框一样在框中绘制框,并自动调整框的大小并将其移动,以使它不会与绘图边界附近的边缘或接触点重叠。但是你的想法听起来也不错。谢谢你的勾号! – emrys57