可拖动的UIView在添加到UIScrollView后停止发布touchesBegan
在Xcode 5.1中,我为iPhone创build了一个简单的testing应用程序 :
结构是: scrollView -> contentView -> imageView -> image 1000 x 1000
的顶部。
而在单视图应用程序的底部,我有七个可拖动的自定义UIView
。
通过touchesXXXX
方法在Tile.m中执行拖动操作。
我的问题是:一旦我添加一个可拖动的瓷砖到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]; } }
touchesBegan
不再被调用Tile
,就好像scrollView
可以屏蔽该事件。
我周围search ,有一个build议,以下面的方法(在我的自定义GameBoard.m )扩展UIScrollView
类:
- (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; }
不幸的是,这并没有帮助,并在debugging器中打印0
。
问题部分是因为用户交互在内容视图上被禁用。 但是,启用用户交互禁用滚动视图捕获所有触摸。 所以这里是解决scheme。 在故事板中启用用户交互,但像这样inheritance内容视图:
@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
这样,只有在接受的视图不是self
的内容视图时才打击testing通过。
这是我的承诺: https : //github.com/LeoNatan/ios-newbie
平铺视图没有被触及的原因是滚动视图的平移手势识别器消耗事件。 你需要的是,附加一个UIPanGestureRecongnizer
到你的每个瓷砖,并configuration它们如下:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; // handle drag in pan:method [tile addGestureRecognizer:pan]; UIPanGestureRecognizer *scrollPan = self.scrollView.panGestureRecognizer; [scrollPan requireGestureRecognizerToFail:pan];
在这里你让滚动视图的平移手势识别器知道你只希望滚动发生,如果没有任何瓷砖被拖动。
我已经检查了方法 – 确实有效。 关于您的代码,您需要处理手势识别器中的所有触摸,而不是“平铺”视图,因为触摸事件可能会在触击事件到达视图本身之前被触击testing视图的手势识别器消耗/延迟。 请参阅UIGestureRecognizer文档以了解有关该主题的更多信息。
它看起来像一个在层次结构中的意见捕捉事件。
看看这个部分
响应者链遵循特定的传递path
在这里的苹果文件
编辑:
对不起,我是从记忆中写的。 这是我在自己的应用程序中解决类似问题的方式:
我在视图中使用UITapGestureRecognizer
来检测触摸。 实现UITapGestureRecognizer的以下委托方法:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
触摸集合包含接收事件的所有对象(视图)。