注意:我已经阅读了一些关于 UIScrollView
向 subview 发送触摸的问题(包括 this,虽然我投了赞成票,但它没有按我的预期工作不再)。
我有什么:我有一个 UIScrollView
,里面有一个自定义 UIView
(我们称它为 A)它涵盖了整个 UIScrollView
。我还可以将其他自定义 UIViews
放入 A。
在我这样做的代码上:
[scrollView setDelaysContentTouches:YES];
scrollView.canCancelContentTouches = NO;
发生了什么:目前我唯一的问题是,如果我想在 A 中移动一个 subview ,我必须触摸它,等待,然后移动它。完全符合说明 here :
Now, the behaviour changes depending on the "length in time" of the first touch on the UIView. If it's short, then the relative dragging is managed as it was a scroll for the UIScrollView. If it's long, then I'm getting the touchesMoved: events inside my UIView.
我想要的:A 内的 subview 应该始终获得优先权,我不应该触摸和等待。如果我触摸 A 而不是它的 subview ,我希望 UIScrollView
接收触摸,比如平移和四处移动(contentSize
是比 frame
大)。
编辑 1.0
我在通用 UIScrollView
中使用此 A View 的唯一原因是因为我希望能够放大/缩小 A View 。所以我正在做以下事情:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return customView; // this is the A view
}
一开始我在 UIScrollView
中没有 A View ,我唯一做的就是添加 A 作为我的 UIViewController 的
Root View 的 subview ,一切顺利。 如果有其他方法可以启用放大/缩小,我会很乐意接受这个答案。
最佳答案
注意:感谢大家的贡献,特别是 Aaron Hayman。
我能够通过对我拥有的 UIScrollView
子类执行以下操作来解决这个问题:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint pointOfContact = [gestureRecognizer locationInView:self];
// The view with a tag of 200 is my A view.
return (![[self hitTest:pointOfContact withEvent:nil] isEqual:[self viewWithTag:200]]);
}
关于iphone - UIScrollView 将触摸发送到 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11947797/