Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

iphone - Dragging an UIView inside UIScrollView

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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().)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...