0

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

我尝试过覆盖touchesBegan,touchesShouldCancelInContentView和 ,touchesShouldBegin因此如果触摸不是源自我调用的工具栏setScrollEnabled:NO,如果是我调用setScrollEnabled:YES,但似乎一旦禁用滚动,方法就不再触发并且我陷入僵局。还尝试将工具栏设置为按钮并启用滚动touchDown和禁用touchUpInsidetouchUpOutside。这也行不通。有人有想法么?

4

1 回答 1

0

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

#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
于 2012-10-22T21:08:45.213 回答