Reputation: 411
Ok, I have a View, and first, I want that as soon as the user touches the View (a rectangle in this case) and starts dragging around a line should be drawn, following the path of the finger. Later, when I call a specific method, I want the View to follow the line and of course the line to disappear.
My thought for drawing the line:
UIPanGestureRecognizer
to the view, and then AddLineToContext
, then draw it.touchesBegan
, etc. methods. But later, I have multiple Views in there, and I need to find out which one the user touched. (have fun, there will be between 1 and 15...)And I still have no idea about the other thing.
Upvotes: 0
Views: 569
Reputation: 9251
For the purpose of drawing the path, you are on the right track. I would use the gesture recognizer out of the two options you have.
To make the rectangle follow the path the easiest method I can think of is to keep an array of x and y for each point you are passing trough (don't forget to remove the consecutive duplicates).
So, now that you have an array of points that describe the shape of the path you can start a timer, or better use CADisplayLink, that will set the position of the rectangle to each of the points in the array. This will make the rectangle follow the path.
If you want the rectangle to follow the orientation as well, you will need to use vectors to describe the direction in which the rectangle should head.
First you need calculate the distance between the rectangle's position and the point in which it should go next using:
then, when you know the distance you can use
arcsine
to get you the direction angle. Then simply rotate the rect by that value.
Be careful at angle representation (pi vs degrees) and at the coordinates system.
Upvotes: 1