-1

i have an image that i drag around in my screen but, no matter where i touch the screen the image come to my finger and start dragging and for all the video i watched and all blog and article all that people are showing is how to do so ... its not what i want .. what i want is to start dragging the image only when i touch the image .. heres my code

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *Drag = [ [  event allTouches ] anyObject ];
Dot.center = [ Drag locationInView: self.view ];

[self checkCollison];

}

thank you

2 Answers2

0

In your .h

IBOutlet UIImageView * YOURIMAGE;

In your .m

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch view] == YOURIMAGE) {
        YOURIMAGE.center = [touch locationInView:self.view];
    }
}
user3078845
  • 3
  • 1
  • 5
0

Instead of using -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event method use UIPanGestureRecognizer.

Create and add a pan gesture to your UIImageView like:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
[yourImageView addGestureRecognizer:panGesture];

And write the selector method like:

- (void)handleDrag:(UIPanGestureRecognizer*)recognizer
{

    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint velocity    = [recognizer velocityInView:recognizer.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
       // start tracking movement
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
         // You can change position here, with animation
    }
 }
Midhun MP
  • 103,496
  • 31
  • 153
  • 200