ios - 禁用 UIScrollView 拖动事件到特定方向并将事件发送到 super
<p><p>我有 <code>UIView</code> 并附加了一个 <code>UIPanGestureRecognizer</code>。
在 <code>UIView</code> 我有 <code>UIScrollView</code> 启用了分页,我设置了内容大小,所以 scrollView 可以滚动 <strong><em></em></strong> 到左边。 </p>
<p><em><strong>问题:</strong></em></p>
<p>我希望当用户尝试将 scrollView 向右拖动时,将事件向上发送到 UIView,以便 'UIPanGestureRecognizer' 可以处理触摸事件</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>终于,6 小时后我弄明白了
我继承了 <code>UIScrollView</code> 并实现了 <code>gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer</code> 委托(delegate)方法</p>
<p>当用户拖动 ScrollView 时,此方法被调用 <code>(BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer</code>,默认返回 NO</p>
<pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (self.contentOffset.x > 0 &&self.contentOffset.x <= self.contentSize.width - self.frame.size.width) {
//user is in middle of dragging the scrollView don't allow any other gestureRecognizer to respond
return NO;
}else{
//scrollView contentOffset is 0 and user is not dragging the scrollView so let other gestureRecognizer to respond
return YES;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 禁用 UIScrollView 拖动事件到特定方向并将事件发送到 super ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18704244/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18704244/
</a>
</p>
页:
[1]