2014-10-30 131 views
20

第一次使用BezierPaths,想知道這個函數實際上應該如何實現。目前貝塞爾路徑在圖像的框架內移動,而不是在屏幕上繪圖。用UIBezierPath繪製一條線

有沒有更好的方法來做到這一點?

func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) { 

    var maxWidth = abs(start.x - end.x) 
    var maxHeight = abs(start.y - end.y) 

    var contextSize : CGSize! 
    if maxWidth == 0 { 
     contextSize = CGSize(width: 1, height: maxHeight) 
    }else { 
     contextSize = CGSize(width: maxWidth, height: 1) 
    } 

    //design the path 
    UIGraphicsBeginImageContextWithOptions(contextSize, false, 0) 
    var path = UIBezierPath() 
    path.lineWidth = 1.0 
    lineColor.set() 

    //draw the path and make visible 
    path.moveToPoint(start) 
    path.addLineToPoint(end) 
    path.stroke() 

    //create image from path and add to subview 
    var image = UIGraphicsGetImageFromCurrentImageContext() 
    var imageView = UIImageView(image: image) 
    view.addSubview(imageView) 
    UIGraphicsEndImageContext() 
} 
+0

其他的,更直接的方法,而不是使用圖像這樣的,被使用'CAShapeLayer '或定製'drawRect',如下所述:http://stackoverflow.com/a/16846770/1271826 – Rob 2014-10-30 20:18:25

回答

38

最後只是這樣:威廉獵鷹的答案

func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) { 

    //design the path 
    var path = UIBezierPath() 
    path.moveToPoint(start) 
    path.addLineToPoint(end) 

    //design path in layer 
    var shapeLayer = CAShapeLayer() 
    shapeLayer.path = path.CGPath 
    shapeLayer.strokeColor = lineColor.CGColor 
    shapeLayer.lineWidth = 1.0 

    view.layer.addSublayer(shapeLayer) 
} 
+1

我沒有使用圖層中的設計路徑。但只是'path.Stroke'。我不明白爲什麼這條線沒有被添加。有什麼建議? @williamFalcon – 2016-01-13 10:14:41

2

雨燕3.1版本+改進

這只是一個更新已接受的答案的版本,我只是增加了一點點它。

func drawLineFromPointToPoint(startX: Int, toEndingX endX: Int, startingY startY: Int, toEndingY endY: Int, ofColor lineColor: UIColor, widthOfLine lineWidth: CGFloat, inView view: UIView) { 

    let path = UIBezierPath() 
    path.move(to: CGPoint(x: startX, y: startY)) 
    path.addLine(to: CGPoint(x: endX, y: endY)) 

    let shapeLayer = CAShapeLayer() 
    shapeLayer.path = path.cgPath 
    shapeLayer.strokeColor = lineColor.cgColor 
    shapeLayer.lineWidth = lineWidth 

    view.layer.addSublayer(shapeLayer) 

} 

當然的實施將

drawLineFromPointToPoint(startX: Int, toEndingX: Int, startingY: Int, toEndingY: Int, ofColor: UIColor, widthOfLine: CGFloat, inView: UIView) 

我從接受的答案改變什麼

我改讓增值經銷商,並使其更容易輸入起點和終點的x和y。我也允許用戶改變線條的寬度。

我選擇的值爲Int類型,但您可以將其更改爲其他允許的選項。

0

要繪製在頂部水平線:

let path = UIBezierPath() 
path.moveToPoint(CGPoint(x: 0, y: 0)) 
path.addLineToPoint(CGPoint(x: yourView.frame.width, y: 0)) 

let shapeLayer = CAShapeLayer() 
shapeLayer.path = path.CGPath 
shapeLayer.strokeColor = UIColor.lightGrayColor().CGColor 
shapeLayer.lineWidth = 0.5 

yourView.layer.addSublayer(shapeLayer) 

要繪製水平線上底部:

let path = UIBezierPath() 
path.moveToPoint(CGPoint(x: 0, y: yourView.frame.height)) 
path.addLineToPoint(CGPoint(x: yourView.frame.width, y: yourView.frame.height)) 

let shapeLayer = CAShapeLayer() 
shapeLayer.path = path.CGPath 
shapeLayer.strokeColor = UIColor.lightGrayColor().CGColor 
shapeLayer.lineWidth = 0.5 

yourView.layer.addSublayer(shapeLayer)