user2677746
user2677746

Reputation:

Simple object dragging in iOS

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:

  1. the .h file.

    @interface ViewController : UIViewController {
    @property (retain, nonatomic) IBOutlet UIImageView *ball;
      }
    @end        
    
  2. 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

Answers (1)

user523234
user523234

Reputation: 14834

What you need to use is the UIPanGestureRecognizer applying to the object itself. Here is good tutorial on this.

Upvotes: 3

Related Questions