0

I need the user to be able to drag a subview across the screen to change the subview's position. I know that it is possible to do it either way, but is one way preferred? And why?

Brett
  • 1
  • Cocoanetics has an example of [draggable buttons and labels](http://www.cocoanetics.com/2010/11/draggable-buttons-labels/). They discuss 3 different methods, so it's worth looking at. –  Sep 20 '11 at 23:54

2 Answers2

1

Based purely on the concept of MVC, whatever is responsible for that view should probably handle the pan gesture recognizer. At least, that's what I do. The view itself shouldn't really care or be aware that it's being moved around.

Now, for something like zooming or scrolling, that does seem relevant to the view (e.g., UIScrollView has the panGestureRecognizer property). But in this case, it's part of the functionality of the view, so it makes sense for the view to control that gesture recognizer.

In general, it's usually about what the intent is and who should semantically be responsible for handling the action. So, in your case, I would add the UIPanGestureRecognizer to the to the superview and let it manage its own subviews.

CIFilter
  • 8,647
  • 4
  • 46
  • 66
0

If you are using iOS 3.2 or higher look into UIPanGestureRecognizer.

// **** other Code ****
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

panGesture.delegate = self;
[dragView addGestureRecognizer:panGesture];
[panGesture release];
}

- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer 
{
    CGPoint location = [gestureRecognizer locationInView:parentView]; // The view the of the drag item is in
    gestureRecognizer.view.center = location;
}

This is all done from memory, but it should be something like this

NixonsBack
  • 390
  • 2
  • 15