objective-c - 如何禁用水平滚动并且页面将在使用 UIScrollView 的分页中单击时更改?

标签 objective-c swift uiscrollview uisegmentedcontrol uistackview

我有一个 UIScrollViewpagingEnabled。添加了 5 个 UIStackViewUIScrollView 的 subview ,还有一个 UISegmentedControl。我想显示当前选定的段,还想禁用水平滚动,但 View 将能够垂直滚动。

举个例子:

  • 让选中的页面索引=0
  • 用户无法通过滚动或水平拖动来导航到下一页。
  • 用户可以垂直滚动
  • 现在用户在 UISegmentedControl 中选择的页面索引 = 1
  • 第二页将可见
  • 现在用户无法滚动或拖动到 0 和 2 索引

如何使用 UIScrollView 实现此功能

尝试过的逻辑:

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];

CGFloat xOff = self.scrollView.frame.size.width * self.currentPage;
[self.scrollView setContentSize:CGSizeMake(xOff, self.scrollView.contentSize.height)];
//[self.scrollView setContentOffset:CGPointMake(xOff, 0) animated:true];

}

但在更改页面后使用此 UIScrollView 当我再次滚动时转到 0 页。

最佳答案

您可以在启用分页的情况下使用 UICollectionView,在内部您可以实现启用垂直滚动的 UIScrollView。使用以下代码禁用 UICollectionView 滚动并更改索引集合单元格:

已接受 新的、编辑过的答案:

在viewDidLayoutSubviews中添加这个

SWIFT

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let indexPath = IndexPath(item: 12, section: 0)
    self.collectionView.scrollToItem(at: indexPath, at: [ .centeredHorizontally], animated: true)
}

通常,.centerVertically 是这样的

Objective-C

-(void)viewDidLayoutSubviews {
   [super viewDidLayoutSubviews];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:12 inSection:0];
   [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

关于objective-c - 如何禁用水平滚动并且页面将在使用 UIScrollView 的分页中单击时更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51630305/

相关文章:

ios - 使用 Objective-C 将 JSON 数据传输到 UITableView

objective-c - 使用子类 NSView 关闭时执行操作

ios - 如何在多个 View Controller 中将图像从 UIImagePickerController 设置为 UIimages

ios - 将 ScrollView 添加到我的 viewController

iOS 10.1 和 Xcode 8 问题

iphone - Objective-c 中的 (void)^(void)?

iOS swift : Get user selected phone number from CNContactProperty as a string

Swift:Spritekit 中的多个场景,从第二个和第一个场景上的不同 Sprite 节点类型调用 TouchBegan

ios - 带分页的 ScrollView

ios - 插入新行时 TableView 滚动到中间