【问题标题】:Embed UITableView and other UIViews inside a restricted UIScrollView在受限的 UIScrollView 中嵌入 UITableView 和其他 UIView
【发布时间】:2014-05-04 19:27:29
【问题描述】:
我的情况有点复杂,涉及多个手势。基本上,我想要一个容器 UIScrollView,如果触摸在特定区域内,它只会从左到右滚动。如果它们不在该区域内,则 UIScrollView 会将这些触摸传递给 UIScrollView 内并排存在的子 UIView(将其视为面板导航)。
我的 UIScrollView 包含 UIViews 工作正常。我继承了 UIScrollView 并通过 TouchesBegan/TouchesMoved/TouchesEnded/TouchesCancelled 添加了平移限制。除非 UIView 是 UITableView,否则一切正常。那时我的父 UIScrollView 似乎永远不会收到这些事件,因此永远无法正确限制平移。
有人对如何实现这一点有任何想法吗?
谢谢!
【问题讨论】:
标签:
ios
objective-c
uitableview
uiscrollview
【解决方案1】:
这样做的方法是子类化正在吃触摸事件的子视图,并且不允许 UIScrollView 获取它们。然后,覆盖 pointInside: 方法(对于您希望仍然工作的 UI 有适当的例外)。例如:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// Confine the offending control to a certain area
CGRect frame = CGRectMake(0, 0,
self.frame.size.width,
self.frame.size.height - 100.00);
// Except for subview buttons (or some other UI element)
if([self depthFirstButtonTest:self pointInside:point withEvent:event])
{
return YES;
}
return (CGRectContainsPoint(frame, point));
}
- (BOOL)depthFirstButtonTest:(UIView*)view pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView * subview in view.subviews)
{
if([self depthFirstButtonTest:subview pointInside:point withEvent:event])
{
return YES;
}
}
// Is it a button? If so, perform normal testing on it
if ([view isKindOfClass:[UIButton class]]) {
CGPoint pointInButton = [view convertPoint:point fromView:self];
if ([view pointInside:pointInButton withEvent:event]) {
return YES;
}
}
return NO;
}