我有一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
创建类似于主视图的跳板Create springboard like main view(创建类似于主视图的跳板)
滑动删除嵌入在 UIScrollView 中的 UITableViewswipe to delete in a UITableView which is embeded in a UIScrollView(滑动删除嵌入在 UIScrollView 中的 UITableView)
检查 UIScrollView 中的滚动方向Check direction of scroll in UIScrollView(检查 UIScrollView 中的滚动方向)
UIScrollView 不滚动?UIScrollView Not Scrolling?(UIScrollView 不滚动?)
UIScrollView 如何从其子视图中窃取触摸?How does UIScrollView steal touches from its subviews?(UIScrollView 如何从其子视图中窃取触摸?)
使用自动布局将子视图的宽度与其父视图匹配Matching subview#39;s width to it#39;s superview using autolayout(使用自动布局将子视图的宽度与其父视图匹配)