我不确定我是否误解了您的要求 - 但这可能是了解如何设置它的起点:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewBounds = self.view.bounds;
CGRect scrollViewFrame = CGRectMake(0, 0, floorf(CGRectGetWidth(viewBounds) / 2.2), CGRectGetHeight(viewBounds));
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.center = self.view.center;
scrollView.contentSize = CGSizeMake(CGRectGetWidth(viewBounds) * 3, CGRectGetHeight(viewBounds) * 3);
scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = NO;
UIPanGestureRecognizer *gestureRecognizer = scrollView.panGestureRecognizer;
[self.view addGestureRecognizer:gestureRecognizer];
for (int i = 0; i < 3; i++) {
CGRect frame = CGRectMake(10.f + (i * CGRectGetWidth(scrollView.bounds)), 10.f, CGRectGetWidth(scrollView.bounds) - 20.f, (CGRectGetHeight(scrollViewFrame) * 3) - 20.f);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor redColor];
[scrollView addSubview:view];
}
[self.view addSubview:scrollView];
}
从字面上看,只是把它放在一个空的 viewController 中viewDidLoad:
需要注意的关键事项是
contentSize
需要足够宽以容纳所有面板
clipsToBounds
应该是NO
这样你就可以看到额外的视图
- 滚动视图的边界本质上是主视图端口
pagingEnabled
应该设置
- 我已经
panGestureRecognizer
从滚动视图中抓取并将其附加到包含视图,以便在包含视图的边界(更大)中检测到平移,否则您只能检测滚动视图边界内的滚动