Tracer une ligne avec UIBezierPath

Première utilisation de BezierPaths, en se demandant comment cette fonction est supposée être implémentée. Actuellement, le path de Bézier se déplace dans le cadre de l'image, par opposition à dessiner sur l'écran.

Y a-t-il une meilleure façon de le faire?

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() } 

Fini par le faire de cette façon:

 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) } 

Swift 3.1 Version de la réponse de William Falcon + Amélioré

Ceci est juste une version mise à jour de la réponse déjà acceptée, et je viens d'append un peu plus.

 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) } 

Et bien sûr, la mise en œuvre serait

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

Ce que j'ai changé de réponse acceptée

J'ai changé les vars pour les laisser, et j'ai facilité le début et la fin du x et du y. Je permets également à l'user de changer la largeur de la ligne.

J'ai choisi pour les valeurs d'être de type Int, mais vous pouvez les changer pour qu'elles soient les autres options autorisées.

Pour dessiner une ligne horizontale sur le dessus:

 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) 

Pour dessiner une ligne horizontale en bas:

 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)