Search code examples
iosobjective-ccocoatouchesmoved

Moving a UIImageView without using the center property


Basically I would just like to touch the image and have it move with my finger across the screen without using theImage.center = touchPosition I don't want to have the center snap to my finger. I want to move the image from whatever point I touch.


Solution

  • The standard way to handle this, which @richard-j-ross-iii hinted at in a comment, is to save the difference between the view's center and the touch position when the drag starts and then maintain that difference as the drag proceeds. In your touchesBegan:withEvent: method save the offset like:

    _dragOffsetFromCenter = CGSizeMake(touchLocationInImageView.x - centerOfImageView.x, touchLocationInImageView.y - centerOfImageView.y);
    

    then in touchesMoved:withEvent: you can maintain that same offset like:

    myImageView.center = CGPointMake(touchLocation.x - _dragOffsetFromCenter.x, touchLocation.y - _dragOffsetFromCenter.y);