问题描述
在Xcode 5.1中,我创建了一个简单的测试应用用于iPhone:
In Xcode 5.1 I have created a simple test app for iPhone:
结构为:顶部为scrollView -> contentView -> imageView -> image 1000 x 1000
.
在单视图应用程序的底部,我有七个可拖动的自定义UIView
.
And on the bottom of the single view app I have seven draggable custom UIView
s.
我的问题是:在我将 ViewController.m 文件-我不能再将其拖动:
My problem is: once I add a draggable tile to the contentView
in my ViewController.m file - I can not drag it anymore:
- (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];
}
}
不再为Tile
调用touchesBegan
,就像scrollView
会掩盖该事件一样.
The touchesBegan
isn't called for the Tile
anymore as if the scrollView
would mask that event.
我已经搜索了,并且有人建议扩展UIScrollView
使用以下方法创建类(在我的自定义 GameBoard.m ):
I've searched around and there was a suggestion to extend the UIScrollView
class with the following method (in my custom 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;
}
不幸的是,这无济于事,并在调试器中显示0
.
Unfortunately this doesn't help and prints 0
in debugger.
推荐答案
问题部分是由于在内容视图上禁用了用户交互.但是,由于视图捕获了所有触摸,因此启用用户交互会禁用滚动.所以这是解决方案.在情节提要中启用用户交互,但将内容视图子类化,如下所示:
The problem is, partly, because user interactions are disabled on the content view. However, enabling user interactions disables scrolling as the view captures all touches. So here is the solution. Enable user interactions in storyboard, but subclass the content view like so:
@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
内容视图时,命中测试才会通过.
This way, hit test passes only if the accepting view is not self
, the content view.
这是我的承诺: https://github.com/LeoNatan/ios-newbie
这篇关于可拖动的UIView在添加到UIScrollView后停止发布touchesBegan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!