0

How can I create a simple line that I can control its originating point, but let the user determine where it ends?

I have a UIView on my storyboard and on top of this I want to insert a line. The line would be restricted to be within this particular view and would start vertically centered and from the left side.

I have buttons with a - and a + to allow the user to increase its length or decrease it.

RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70

2 Answers2

0

I suppose the easiest method will be to a add another UIView (call it lineView) inside your main view, set it's width to whatever you want the width of your line to be and hook it up with an @IBOutlet. Then in your @IBAction methods for - and +, change the height of lineView

Malik
  • 3,763
  • 1
  • 22
  • 35
0

I found the answer here, How to draw a line between two points over an image in swift 3?

class LineView : UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(white: 0.0, alpha: 0.0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            context.setStrokeColor(UIColor.blue.cgColor)
            context.setLineWidth(3)
            context.beginPath()
            context.move(to: CGPoint(x: 5.0, y: 5.0)) // This would be oldX, oldY
            context.addLine(to: CGPoint(x: 50.0, y: 50.0)) // This would be newX, newY
            context.strokePath()
        }
    }
}


let imageView = UIImageView(image: #imageLiteral(resourceName: "image.png")) // This would be your mapView, here I am just using a random image
let lineView = LineView(frame: imageView.frame)
imageView.addSubview(lineView)
Community
  • 1
  • 1
RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70