Это можно сделать с помощью событий touchesBegan и touchesMoved.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// This gets you starting position of
UITouch *touch = [ [ event allTouches ] anyObject ] ;
float touchXBeginPoint = [ touch locationInView:touch.view ].x ;
float touchYBeginPoint = [ touch locationInView:touch.view ].y ;
// Calculate the offset between the current image center and the touched points.
// Moving image only along X - direction and try thinking as how to move in
// any direction using this as a reference. It isn't that hard.
touchOffset = image.center.x - touchXBeginPoint ;
// touchOffset should be a member variable of class or a variable with global scope
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Calculate the difference from previous position and the current position
// Add this difference to the previous point and move the image center to that point.
// How ever, you should have an UIImageView outlet connected on to the image placed
// on the interface builder.
// And regarding image movement restriction, since you always have co-ordinates with
// you, you can set the boundaries.
UITouch *touch = [ [ event allTouches ] anyObject ] ;
float distanceMoved =( [ touch locationInView:touch.view ].x + touchOffset ) - image.center.x ;
float newX = image.center.x + distanceMoved ;
if( newX > 30 && newX < 290 ) // setting the boundaries
image.center = CGPointMake(newX, image.center.y) ;
}
Надеюсь, это будет полезно.