【问题标题】:UIScrollView with active and inactive areas具有活动和非活动区域的 UIScrollView
【发布时间】:2012-10-22 19:59:43
【问题描述】:

我有一个UIScrollView,它占据了我的整个屏幕,一侧有一个垂直工具栏,我需要限制滚动行为,以便用户只能通过拖动工具栏来滚动。如果他们尝试从屏幕上的任何其他位置拖动,则不应发生滚动。

我尝试过覆盖touchesBegantouchesShouldCancelInContentViewtouchesShouldBegin,以便如果触摸不是源自工具栏,我调用setScrollEnabled:NO,如果是,我调用setScrollEnabled:YES,但似乎就像禁用滚动一样,方法不再触发,我陷入僵局。还尝试将工具栏设置为按钮并在touchDown 上启用滚动并在touchUpInsidetouchUpOutside 上禁用。这也行不通。有人有什么想法吗?

【问题讨论】:

    标签: ios uiview uiscrollview scroll


    【解决方案1】:

    使用第二个滚动视图来控制主滚动视图。我添加了一个渐变,以便能够看到滚动。

    #import <QuartzCore/QuartzCore.h>
    
    @implementation ScrollViewController {
        UIScrollView *mainScrollView;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        CGRect mainScrollViewRect = CGRectMake(0, 0, self.view.bounds.size.width - 100, self.view.bounds.size.height);
        mainScrollView = [[UIScrollView alloc] initWithFrame:mainScrollViewRect];
        mainScrollView.scrollEnabled = NO;
        mainScrollView.contentSize = CGSizeMake(self.view.bounds.size.width - 100, 1000);
        [self.view addSubview:mainScrollView];
    
        CAGradientLayer* gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 1);
        gradientLayer.frame = CGRectMake(0, 0, mainScrollView.contentSize.width, mainScrollView.contentSize.height);
        gradientLayer.colors = @[(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor]];
        [mainScrollView.layer insertSublayer:gradientLayer atIndex:0];
    
        CGRect toolbarRect = CGRectMake(self.view.bounds.size.width - 100, 0, 100, self.view.bounds.size.height);
        UIScrollView *toolbarScrollView = [[UIScrollView alloc] initWithFrame:toolbarRect];
        toolbarScrollView.delegate = self;
        toolbarScrollView.contentSize = CGSizeMake(0, 1000);
        toolbarScrollView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:toolbarScrollView];
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        mainScrollView.contentOffset = scrollView.contentOffset;
    }
    
    @end
    

    【讨论】:

    • 我想我最初的描述不够清楚。工具栏不是滚动视图;它只是一个锚点,用户可以单击它然后在屏幕上拖动,就像通过从末端拖动它来打开窗帘一样。有意义吗?
    • 不是真的,你有你想要做什么的形象吗?你说的工具栏像窗帘是指垂直滑块吗?
    • 让我重新开始。该应用程序以横向模式启动,滚动视图填满整个屏幕,左侧边缘有一个垂直“条”。滚动视图的内容区域是屏幕宽度的两倍,但用户应该只能通过将条从左侧拖动到右侧(然后再返回)来滚动。应忽略其他任何地方的滑动手势。我最初称它为“工具栏”,因为它上面会有与其他功能相关的按钮。
    猜你喜欢
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2018-01-31
    • 2019-11-11
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多