2016-03-10 96 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

相關問題