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
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
