Iphone cocoa: how to move the image along the way
What you are basically trying to do is move your finger to the "translate" transition.
When the user touches and starts moving their finger, you want to use the current touchpoint value to create the broadcast transform that you apply to yours UIImageView
. Here's how you do it:
-
When tapping down, save the image starting at the x, y position.
-
On the move, calculate the delta from the old point to the new one. This is where you can pin the values. This way you can ignore, say, changing y and only use x deltas. This means that the image will only move from left to right. If you ignore x and use y, then it moves up and down.
-
Once you have "new" computed / clamped x, y values, use it to create a new transform with
CGAffineTransformMakeTranslation
(x, y). Assign this transformation to the UIImageView. The image moves to that location. -
Once the finger is lifted, determine the delta from the original start x, y, point and breakout point, then adjust the bounds of the ImageView and reset the transform to
CGAffineTransformIdentity
. This does not move the object, but it sets it up so that subsequent calls to the ImageView use the actual position and do not need to be adjusted for transformations.
Moving around the grid is easy too. Just round off the x, y values in step 2 to keep them short in grid size (that is, round to every 10 pixels) before submitting it for broadcast conversion.
If you want to make it smoother, combine the code where you assign the transition with the UIView animation blocks. Worry with attenuation and sync settings. The image should drag a little, but smoothly, the rubber band from one touch point to the next.
a source to share