可拖动的UIView在添加到UIScrollView后停止发布touchesBegan

Ale*_*ber 4 iphone objective-c uiscrollview touchesbegan ios

在Xcode 5.1中,我为iPhone 创建了一个简单的测试应用程序:

应用截图

结构是:scrollView -> contentView -> imageView -> image 1000 x 1000在顶部.

在单一视图应用程序的底部,我有七个可拖动的自定义UIViews.

使用方法在Tile.m中实现拖动touchesXXXX.

我的问题是:一旦我contentView在我的ViewController.m文件中添加了一个可拖动的磁贴- 我就不能再拖动了它:

- (void) handleTileMoved:(NSNotification*)notification {
    Tile* tile = (Tile*)notification.object;
    //return;

    if (tile.superview != _scrollView && CGRectIntersectsRect(tile.frame, _scrollView.frame)) {
        [tile removeFromSuperview];
        [_contentView addSubview:tile];
        [_contentView bringSubviewToFront:tile];
    }
}
Run Code Online (Sandbox Code Playgroud)

touchesBegan没有呼吁Tile了,因为如果scrollView将掩盖这一事件.

我已经四处搜索,并建议UIScrollView使用以下方法扩展该类(在我的自定义GameBoard.m中):

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];

    NSLog(@"%s: %hhd", __PRETTY_FUNCTION__,
          [result.superview isKindOfClass:[Tile class]]);

    self.scrollEnabled = ![result.superview isKindOfClass:[Tile class]];
    return result;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这没有帮助,并0在调试器中打印.

Leo*_*ica 6

问题在于,部分原因是因为内容视图上禁用了用户交互.但是,启用用户交互会在视图捕获所有触摸时禁用滚动.所以这是解决方案.在storyboard中启用用户交互,但是像这样子类化内容视图:

@interface LNContentView : UIView

@end

@implementation LNContentView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];

    return result == self ? nil : result;
}

@end
Run Code Online (Sandbox Code Playgroud)

这样,仅当接受视图不是self内容视图时,命中测试才会通过.

这是我的提交:https: //github.com/LeoNatan/ios-newbie