Drag object with touchesMoved along offset vector / delta?
In the touchesMoved method, what is the math to have an object on the screen move along a dragging touch vector? For instance, if I touch beneath my object (Y > object.y), and drag "up", I want the object to move from its current x,y location along the same vector that my finger moves.
Currently I have it working that the drag motion snaps my object to my finger's location, but sometimes my finger hits the edge of the screen before I get my object positioned correctly.
If I know my "touch down location" and the "current touch location" what is the math to determine where to move object.positio开发者_运维知识库n
?
I hope that you have already received the answer of figured it out yourself, but.
Assume that your object's Upper Left Corner (ULC) is at Object.X
, ObjectY
.
Assume that the CGPoint
returned by touchsBegan
is Touch.X
, Touch.Y
.
The offset (Delta) will be:
Delta.X = Touch.X - Object.X
Delta.Y = Touch.Y - Object.X
Subtract these from each point returned by touchesMoved
and your ULC should move as you expect rather than snapping to your finger.
Good luck.
I would look into UIPanGestureRecognizer. It's what I use when I want to accomplish something like what you are describing.
This is what I used to learn about them but then took it further in other forms: http://www.cocoanetics.com/2010/11/draggable-buttons-labels/
Good luck!
精彩评论