ios - 将触摸转发到 UIScrollView

标签 ios iphone cocoa-touch uiscrollview uigesturerecognizer

我有两个 View Controller 。 View Controller A 有一个 UIScrollView 并呈现 View Controller B。呈现是交互式的,由 scrollView.contentOffset 控制。

我想集成一个交互式关闭过渡:向上平移时,ViewController B 应该以交互方式关闭。交互式关闭过渡还应该控制 ViewController ScrollView 。

我的第一次尝试是使用 UIPanGestureRecognizer 并根据平移距离设置 scrollView.contentOffset。这是有效的,但是当平移手势结束时,必须将 scrollView 偏移动画化到结束位置。使用 -[UIScrollView setContentOffset:animated: 不是一个好的解决方案,因为它使用线性动画,没有考虑当前的平移速度,也没有很好地减速。

所以我认为应该可以将我的平移手势识别器中的触摸事件提供给 ScrollView 。这应该为我们提供所有漂亮的 ScrollView 动画行为。

我尝试在我的 UIPanGestureRecognizer 子类中覆盖 -touchesBegan/Moved/Ended/Cancelled withEvent: 方法,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [scrollView touchesBegan:touches withEvent:event];
    [scrollView.panGestureRecognizer touchesBegan:touches withEvent:event];

    [super touchesBegan:touches withEvent:event];
}

但显然有些东西阻止 ScrollView 进入 tracking 模式。 (它确实转到 dragging = YES 但仅此而已。)我验证了 scrollView 是 userInteractionEnabled,没有隐藏并添加到 View 层次结构中。

那么如何将我的触摸事件转发到 UIScrollView

最佳答案

阅读 interesting answer 之后描述了 UIScrollView 的事件流,我得出的结论是,尝试从手势识别器“远程控制” ScrollView 可能很难实现,因为触摸在被路由到 View 和手势时会发生变化识别器。由于 UITouch 不符合 NSCopying,我们也无法克隆触摸事件以便稍后以未修改的状态发送它们。

虽然没有真正解决我要求的问题,但我找到了一个解决方法来完成我需要的。我刚刚添加了一个 ScrollView 到 View Controller B 并将其与 VC A 的 ScrollView 同步(垂直滚动时添加到 View 层次结构):

// delegate of VC B's scrollView
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
    scrollViewA.contentOffset = scrollView.contentOffset;
}

感谢 Friedrich Markgraf came up with the idea .

关于ios - 将触摸转发到 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21554327/

相关文章:

ios - 为UIImagePickerController设置等效的标签

ios - 在 swift 中为标签设置尾部缩进

iphone - 核心图 x 轴标签重叠/CPTLabellingPolicyAutomatic 不会显示日期

c# - 如何缩小我的单点触摸应用程序的大小

iphone - 使用 UIRequiredDeviceCapabilities Corona SDK 将应用程序提交到 App Store 时出错

ios - 如何修复从结果类型为 'RecipeCell *' 的函数返回 'UITableViewCell *' 的不兼容指针类型?

iphone - iOS 复制目录包括子目录

ios - 作为 Y 的子类的类型 X 的向下转换集

ios - 无论如何,可以在iOS中内嵌播放视频吗?

ios - 为什么我不能在 UITableViewCell 内的 UIView 中设置两个顶角圆角?