如何使滚动视图,滚动在特定的页面时滚动iOS



我正试图以编程方式使scrollview在特定页面上滚动。我还使用了一个pageControl和和工作得很好。我在滚动时遇到问题。

我是这样创建scrollview的:
contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, lengthBarView.frame.size.height + lengthBarView.frame.origin.y, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 143)];
contentScrollView.backgroundColor = [UIColor greenColor];
[self addSubview:contentScrollView];
下面是我创建Page控件的方法:
self.pageControl = [[UIPageControl alloc] init] ;
self.pageControl.frame = CGRectMake(110,385,100,20);
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.pageControl];
pageControl.pageIndicatorTintColor = [UIColor redColor];

下面是我的Page控件函数的代码:

- (void)changePage:(id)sender {
// update the scroll view to the appropriate page
[secondTabNextView.contentScrollView endEditing: YES];
CGRect frame;
frame.origin.x = secondTabNextView.contentScrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = secondTabNextView.contentScrollView.frame.size;
[secondTabNextView.contentScrollView scrollRectToVisible:frame animated:YES];
}

,这里是我的代码scrollview:

- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = secondTabNextView.contentScrollView.frame.size.width;
int page = floor((secondTabNextView.contentScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}

如何在特定页面中滚动,例如从幻灯片中拖动图片

最后修复了它,我只是忘记在我的scrollview中添加分页和滚动启用:

contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, lengthBarView.frame.size.height + lengthBarView.frame.origin.y, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 143)];
contentScrollView.backgroundColor = [UIColor greenColor];
contentScrollView.scrollEnabled = YES;
contentScrollView.pagingEnabled = YES;
[self addSubview:contentScrollView];

最新更新