iOS dragging UIImageView
Asked Answered
T

3

5

Ok, so I have always wondered how do I actually pick up an image view and drag it. I was planning when I drag an image view and when user places it to correct location it locks there. I really don't have idea how to do this and it has bothered me for sometime.

Thanks so much in advance!

Tremolite answered 27/6, 2011 at 22:12 Comment(0)
L
3

If you're one for reading, then check out the UIResponder Class Reference, and, in particular, touchesBegan and touchesMoved.

Lenoralenore answered 27/6, 2011 at 22:18 Comment(4)
So I should implement that method and track the coordinates of the touch and then move the imageView?Tremolite
@Samuli: Yes... there are some good snippets out there (e.g. this one) that have examples, but basically you catch the event with touchesBegan and follow it with touchesMoved or touchesEnded.Lenoralenore
Ok I think I actually get it now. I guessed it would be a lot harder :) Thanks!Tremolite
@Samuli: Me, too. I just started working with these methods a couple of weeks ago, and I must say you can do some amazing stuff with very little code. Good luck with your project!Lenoralenore
P
7

Or you can use a UIPanGestureRecognizer if you don't want to subclass the view and handle touches yourself.

Create a pan recognizer in any class (e.g. view controller) and add it to your view:

UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(handlePanGesture:)];
[myDraggedView addGestureRecognizer:panRecognizer];

And then simply:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGRect frame = myDraggedView.frame;
    frame.origin = [gestureRecognizer locationInView:myDraggedView.superview];
    myDraggedView.frame = frame;
}

If you like Interface Builder as much as me you can also just drag a pan gesture recognizer over your image view, and then connect the recognizer to the handlePanGesture: that should be declared as an IBAction instead of void.

Pluperfect answered 23/5, 2013 at 7:21 Comment(0)
L
3

If you're one for reading, then check out the UIResponder Class Reference, and, in particular, touchesBegan and touchesMoved.

Lenoralenore answered 27/6, 2011 at 22:18 Comment(4)
So I should implement that method and track the coordinates of the touch and then move the imageView?Tremolite
@Samuli: Yes... there are some good snippets out there (e.g. this one) that have examples, but basically you catch the event with touchesBegan and follow it with touchesMoved or touchesEnded.Lenoralenore
Ok I think I actually get it now. I guessed it would be a lot harder :) Thanks!Tremolite
@Samuli: Me, too. I just started working with these methods a couple of weeks ago, and I must say you can do some amazing stuff with very little code. Good luck with your project!Lenoralenore
B
0

I already worked with this, but the user can drag imageview freely with position that they want. and this is my custom class with swift

class DraggableImageView: UIImageView {
      var beganPoint: CGPoint? = nil
      var originCenter: CGPoint? = nil
      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let position = touches.first?.location(in: superview){
                beganPoint = position
                originCenter = center
           }
      }
      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      }
      override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let position = touches.first?.location(in: superview){
                let newPosition = CGPoint(x: position.x - (beganPoint?.x)!, y: position.y - (beganPoint?.y)!)
                center = CGPoint(x: (originCenter?.x)! + newPosition.x, y: (originCenter?.y)! + newPosition.y)
           }
      }
}

Just user this custom class with your UIImageView

Buddleia answered 6/7, 2017 at 4:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.