Draw a line with UIBezierPath

前端 未结 4 1907
礼貌的吻别
礼貌的吻别 2020-12-08 04:22

First time using BezierPaths, wondering how this function is actually supposed to be implemented. Currently the bezier path moves within the frame of the image, as opposed t

4条回答
  •  眼角桃花
    2020-12-08 04:53

    To draw horizontal line on top:

    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)
    

    To draw horizontal line on bottom:

    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)
    

提交回复
热议问题