Сначала создайте контейнер, который будет заботиться о событиях касания.Затем вам нужен подкласс UIIMageView, который будет гарантировать, что когда вы устанавливаете центр объекта, он не выходит за пределы экрана.
Я создал такой класс некоторое время назад, воткод (он также может быть анимируемым).
.h
#import <UIKit/UIKit.h>
@interface ObjectView : UIImageView
{
}
-(void) setCenter: (CGPoint) center inBounds: (CGRect) bounds withAnimation: (BOOL) animate;
@end
.m
#import "ObjectView.h"
@implementation ObjectView
#define MOVE_TO_CENTER_DURATION 0.1
-(void) setCenter: (CGPoint) center inBounds: (CGRect) bounds withAnimation: (BOOL) animate
{
CGRect frame = self.frame;
if(center.x + frame.size.width/2 > bounds.origin.x + bounds.size.width)
center.x -= ((center.x + frame.size.width/2) - (bounds.origin.x + bounds.size.width));
else if(center.x - frame.size.width/2 < bounds.origin.x)
center.x += bounds.origin.x - (center.x - frame.size.width/2);
if(center.y + frame.size.height/2 > bounds.origin.y + bounds.size.height)
center.y -= (center.y + frame.size.height/2) - (bounds.origin.y + bounds.size.height);
else if(center.y - frame.size.height/2 < bounds.origin.y)
center.y += bounds.origin.y - (center.y - frame.size.height/2);
if(animate) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: MOVE_TO_CENTER_DURATION];
}
self.center = center;
if(animate)
[UIView commitAnimations];
}
@end
Надеюсь, это немного поможет!