如何制作滚动视图,滚动iOS时滚动特定页面

时间:2014-08-21 08:40:26

标签: ios uiscrollview uipagecontrol

我正在尝试以编程方式在特定页面上滚动滚动视图。我还使用了pageControl,并且运行良好。我在滚动时遇到了问题。

这是我创建滚动视图的方式:

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];

这是我创建页面控件的方式:

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];

以下是我的页面控件功能的代码:

- (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;
}

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

1 个答案:

答案 0 :(得分:1)

最后解决了,我只忘了在我的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];