[Solved] How to draw dynamic line path on imageView?


First of all I must say your question was not very clear, it took me a while to figure out what you actually want to achieve. I hope I understand correctly. Without simply typing out the actual code you need, this is how I would approach it:

I would create an array with dictionaries containing the letter as key and the point as value, looking something like this:

[
["A": CGPoint(x: 0, y: 0)],
["B": CGPoint(x: 0, y: 0)],
["C": CGPoint(x: 0, y: 0)],
["D": CGPoint(x: 0, y: 0)],
["E": CGPoint(x: 0, y: 0)],
["F": CGPoint(x: 0, y: 0)],
]

(or nicer, this array could have custom models containing similar info)

Then, use this array to plot the UIButtons on the view, giving each UIButton a reference to the item in the array by subclassing it, e.g the key of the dictionary.

CustomButton: UIButton { 
    var key: String?
}

Then when tapping the first button, store that key in a local variable named start and when tapping the second button store that key in a local variable named end. Subsequently, loop through the array and find the matching start key and use the values in the array to draw the lines until you find the matching end key.

Since I was in the mood, I created a simple playground demo, but this is usually not the way it works. You don’t just ask for sample code, you ask for help to solve a problem.

Anyway, you can find the demo here: https://gist.github.com/pvroosendaal/f1617fe7e164bc94f0d37d3175252e2f

By the way, this is by far the crappiest piece of code I wrote, but it does what you want.

I hope it makes sense, otherwise, please let me know.

2

solved How to draw dynamic line path on imageView?