UIScrollView的分页功能只能水平滑动,不能垂直滑动吗?

4

我一直在研究UIScrollView。基本上,我已经将7个视图放入其中,这些视图都比contentSize高。我将分页启用为是,并且水平方向上工作得很好。然而,一个意想不到的副作用是它也在垂直方向上启用了分页。我的高视图以contentSize高度的倍数停止滚动。我希望它可以在垂直方向上正常滚动,并在水平方向上进行分页。这可能吗?

谢谢


请查看此链接: https://dev59.com/KGPVa4cB1Zd3GeqP6ope#10480071 - LK__
3个回答

1
不是我知道的。根据文档,你应该子类化UIScrollView:
子类可以重写touchesShouldBegin:withEvent:inContentView:、pagingEnabled和touchesShouldCancelInContentView:方法(由滚动视图调用),以影响滚动视图处理滚动手势的方式。

1
一个滚动视图只有在某个维度上的contentSize与相同维度上的frame大小不相等时才允许滚动。此外,大小需要是偶数。
因此,在您的示例中,请确保适当设置contentSize以使框架高度。这将防止垂直滚动,无论您在滚动视图中放置了什么内容。

0

"Cocoanetics" 是对的。尝试这段代码。

uiScollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 360, 340)]; view.scrollEnabled = YES; view.pagingEnabled = YES; int y = 20; for (int i = 0; i < ; i++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal]; int x = 0; if (i % 2 == 0) { x = 20; } else { x = 190; } [btn setFrame:CGRectMake(x, y, 170 * 0.8, 170 * 0.8)]; btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; if (i % 2 == 1) { y = y + 170; } [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; btn.tag = i; [view addSubview:btn]; } [view setContentSize:CGSizeMake(360, y + 150)]; [view scrollRectToVisible:CGRectMake(0, 0, 360, 340) animated:YES]; view.scrollEnabled = TRUE; self.view = view; self.view.backgroundColor = [UIColor clearColor]; [view release];


这对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接