UIScrollView inside UIScrollView(UIScrollView 里面的 UIScrollView)
问题描述
我有一个 UIScrollView
和另一个 UIScrollView
里面.它们都是水平滚动的并且具有 pagingEnabled = YES
.假设我开始滚动内部滚动视图并到达最右边的边界.如果我继续在其中滚动,则外部 scrollView 开始移动.我需要避免这种情况.内视图应以橡皮筋效果跳跃,外视图应保持原位.
I have a UIScrollView
with another UIScrollView
inside. They both are scrolled horizontally and have pagingEnabled = YES
.
Assume that I started to scroll inner scroll view and reached the most right bound. And if I proceed scrolling in it, then the outer scrollView begins to move. I need to avoid this. Inner view should jump with rubber-band effect, outer should stay at it's place.
希望很清楚,但这里有一个草图:
Hope it's clear, but here is a sketch:
我试过设置 outerView.scrollEnabled = NO;
像这样:
I've tried to set outerView.scrollEnabled = NO;
like this:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
outerView.scrollEnabled = NO;
}
,它完全按照我的需要工作,如果只是在 innerView 中滚动.OuterView 不再滚动.但是如果我想再次滚动outerView,我必须在某处将scrollEnabled
设置回YES.我在这里尝试过:
, and it works exactly how I need, if to scroll just in innerView. OuterView is not scrolled anymore. But I have to set scrollEnabled
back to YES somewhere for the case if I'd want to scroll outerView again.
I've tried to do it here:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
outerView.scrollEnabled = YES;
}
,但是我遇到了同样的问题:在到达 innerView 的最右侧边界后,outerView 滚动而不是 innerView 以橡皮筋效果跳转.
, but than I'm getting the same problem: after reaching the the most right bound of innerView outerView scrolls instead of innerView jumps with rubber-band effect.
有什么解决问题的建议吗?
Any suggestions how to solve a problem?
推荐答案
更新
此解决方案始终有效:
@implementation InnerScrollViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>
- (void)viewDidLoad
{
UISwipeGestureRecognizer* swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)] autorelease];
swipeGesture.delegate = self;
[self.view addGestureRecognizer:swipeGesture];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
_outerScrollView.scrollEnabled = NO;
return YES;
}
- (void)handleSwipe:(UIGestureRecognizer*)gestureRecognizer
{
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
_outerScrollView.scrollEnabled = NO;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
_outerScrollView.scrollEnabled = YES;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
_outerScrollView.scrollEnabled = YES;
}
@end
---------------------------------------------------------------------------------
旧答案:并不总是有效
这是我解决问题的方法:
Here is how I solved the problem:
@implementation InnerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delaysContentTouches = NO;
}
return self;
}
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
return NO;
}
据我了解,self.delaysContentTouches = NO;
使所有事件立即传递,并且 - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)eventinContentView:(UIView *)view
阻止响应者链传递这些事件.
As I understand, self.delaysContentTouches = NO;
makes all events to be delivered immediately, and - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
prevents passing of these events by responder chain.
这篇关于UIScrollView 里面的 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 里面的 UIScrollView
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01