在UIScrollView中拖动UIView [英] Dragging an UIView inside UIScrollView

查看:97
本文介绍了在UIScrollView中拖动UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图解决iPhone上拖放的基本问题。这是我的设置:

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


  • 我有一个UIScrollView,它有一个大内容子视图(我可以滚动和缩放它)

  • 内容子视图有几个小图块作为子视图,应该在其中拖动。

我的UIScrollView子类有这种方法:

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;
}

平铺视图有这种方法:

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

    self.center = location;
}

这有效,但不完全正确:瓷砖有时会掉落拖动过程。更确切地说,它停止接收touchesMoved:invocations,滚动视图开始滚动。我注意到这取决于拖动速度:我拖得越快,瓷砖下降越快。

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?

推荐答案

解决:原来应该还有touchesBegan:和touchesEnded:实现(在我的情况下有空方法)在平铺中,否则手势开始传播到父视图,并且他们以某种方式拦截手势。对阻力速度的依赖性是虚构的。

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.

这篇关于在UIScrollView中拖动UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆