Dragging an UIView inside UIScrollView

Posted by Sergey Mikhanov on Stack Overflow See other posts from Stack Overflow or by Sergey Mikhanov
Published on 2009-09-26T18:23:01Z Indexed on 2010/06/15 2:12 UTC
Read the original article Hit count: 368

Filed under:
|
|

Hello community!

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?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c