Reputation:
I created a simple app that drags an object any where on the screen, It's great but my issue is when I drag on the screen without touching the object it's position changes to the top corner of the screen, but what I need is to drag the object by touching it and moving it any where, not moving my finger on the screen and the object moved to the top corner, here is my code:
the .h
file.
@interface ViewController : UIViewController {
@property (retain, nonatomic) IBOutlet UIImageView *ball;
}
@end
the .m
file
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *userTouch = [[event touchesForView:_ball] anyObject];
_ball.center = [userTouch locationInView:self.view];
}
So how to fix that issue?
Upvotes: 2
Views: 1494
Reputation: 14834
What you need to use is the UIPanGestureRecognizer applying to the object itself. Here is good tutorial on this.
Upvotes: 3