UIScrollView 里面的 UIScrollView

时间:2022-12-28
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:UIScrollView 错误的偏移与自动布局 下一篇:UISlider 和 UIScrollView

相关文章

最新文章