Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dragging an UIView inside UIScrollView

Tags:

I am trying to solve a basic problem with drag and drop on iPhone. Here's my setup:

  • I have a UIScrollView which has one large content subview (I'm able to scroll and zoom it)
  • Content subview has several small tiles as subviews that should be dragged around inside it.

My UIScrollView subclass has this method:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {     UIView *tile = [contentView pointInsideTiles:[self convertPoint:point toView:contentView] withEvent:event];     if (tile) {         return tile;     } else {         return [super hitTest:point withEvent:event];     } } 

Content subview has this method:

- (UIView *)pointInsideTiles:(CGPoint)point withEvent:(UIEvent *)event {     for (TileView *tile in tiles) {         if ([tile pointInside:[self convertPoint:point toView:tile] withEvent:event])             return tile;     }      return nil; } 

And tile view has this method:

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {     UITouch *touch = [touches anyObject];        CGPoint location = [touch locationInView:self.superview];      self.center = location; } 

This works, but not fully correct: the tile sometimes "falls down" during the drag process. More precisely, it stops receiving touchesMoved: invocations, and scroll view starts scrolling instead. I noticed that this depends on the drag speed: the faster I drag, the quicker the tile "falls".

Any ideas on how to keep the tile glued to the dragging finger?

like image 580
Sergey Mikhanov Avatar asked Sep 26 '09 18:09

Sergey Mikhanov


Video Answer


2 Answers

I was struggling with this same problem - I was trying to do a interface with a lot of "cards" (UIView subclasses) on a cork board, and have the cork board area scrollable, but still able to drag-and-drop the cards. I was doing the hitTest() solution above, but one of the Apple engineers asked me why I was doing it that way. The simpler solution they suggested was as follows:

1) In the UIScrollView class, set the value of canCancelContentTouches to NO - this tells the UIScrollView class to allow touches within subviews (or, in this case, in subviews of subviews).

2) In my "card" class, set exclusiveTouch to YES - this tells the subview it owns the touches inside of it.

After this, I was able to drag around the cards and still scroll the subview. It's a lot simpler and cleaner than the hitTest() solution above.

(BTW, for extra credit, if you are using iOS 3.2 or 4.0 or later, use the UIPanGestureRecognizer class to handle the drag and drop logic - the drag and drop motion is a lot smoother than overriding touchesBegan()/touchesMoved()/touchesEnded().)

like image 101
frauen1 Avatar answered Sep 17 '22 12:09

frauen1


Solved: it turned out that there should be also touchesBegan: and touchesEnded: implementations (in my case having empty methods helped) in the tile, otherwise the gesture started propagating to parent views, and they were intercepting the gesture somehow. Dependency on the drag speed was imaginary.

like image 37
Sergey Mikhanov Avatar answered Sep 20 '22 12:09

Sergey Mikhanov