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?
2 Answers
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.

- 8,647
- 4
- 46
- 66
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

- 390
- 2
- 15