0

I want my UIViewImages to be movable with touch. I'm trying to use code implemented in my ViewController:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count]==1) {
        UITouch *touch = [touches anyObject];
        CGPoint p0 = [touch previousLocationInView:self.view];
        CGPoint p1 = [touch locationInView:self.view];
        CGPoint center = self.view.center;
        center.x += p1.x - p0.x;
        center.y += p1.y - p0.y;
        self.view.center = center;
    }
}

When I try to drag an UIImageView, I'm dragging whole screen, which is incorrect. Need help!

Karol

thedazed
  • 81
  • 1
  • 7

2 Answers2

1

You create a gesture recognizer and add it to a view like this:

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognizerMethod:)];
[self.imageView addGestureRecognizer:panGestureRecognizer];

you can adjust the position of the image view

- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint touchLocation = [recognizer locationInView:self.view];
        self.imageView.center = touchLocation;
    }
}

read this article.

Daxesh Nagar
  • 1,405
  • 14
  • 22
  • Thank you for your feedback. I already tried this - it's neat solution and it works fine, but it forces me to use self.imageView which is already assigned to one of the UIImageViews, while I want to use more than one, and I expect for recognizer to detect which image was tapped and dragged. Or maybe I missed something and I'm wrong? – thedazed Jul 23 '14 at 19:55
  • Already found solution, thank you. The problem I had was using touchesBegan and touchesMoved in ViewController - I moved it to new class I created, derived from UIImageView. Also, I had to override canBecomeFirstResponder function so it returns YES; I set userInteractionEnabled property to YES in viewDidLoad. The solution I found can be found here, where it is nicely explained : [link](http://stackoverflow.com/questions/855095/how-can-i-detect-the-touch-event-of-an-uiimageview/855672#855672) . – thedazed Jul 23 '14 at 22:06
0

If I read your code right self.view actually IS the whole screen. Maybe you mean self.yourImageView instead?

  • Damn, you're right… Is there way to get reference to touched object? I want to have multiple UIImageViews and move them around with fingers. – thedazed Jul 23 '14 at 19:15
  • Of course - your image view is already the reference... Create a property for it if you want to access it anyplace in your code. If you use IB / Storyboard just control drag from your image view to your header and Xcode will create the outlet for you. After that you can reference it with self.someImageView – Bernhard Grabowski Jul 23 '14 at 19:18
  • Daxesh Nagar has written a nicer answer for you. – Bernhard Grabowski Jul 23 '14 at 19:22