我有一个UIScrollView
占据了我的整个屏幕,一边是垂直工具栏,我需要约束滚动行为,这样用户只能通过拖动工具栏进行滚动。如果他们试图从屏幕上的任何其他位置拖动,则不应进行滚动。
我已尝试覆盖touchesBegan
,touchesShouldCancelInContentView
和touchesShouldBegin
,以便如果触摸不是来自工具栏,我会调用setScrollEnabled:NO
,如果是的话调用setScrollEnabled:YES
,但似乎一旦滚动被禁用,方法就不再触发,我就陷入僵局。还尝试将工具栏设置为按钮并启用touchDown
上的滚动并停用touchUpInside
和touchUpOutside
。这也行不通。有人有什么想法吗?
答案 0 :(得分:0)
使用第二个scrollview控制主滚动视图。我添加了一个渐变,以便能够看到滚动。
#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