将UIPageControl添加到UIScrollView

我有一个包含5个图像的UIScrollView。 我想在我的滚动视图的底部添加一个UIPageControl,以便用户可以看到他们在哪个页面上。

这是我的滚动视图的代码:

self.helpScrollView.contentSize = CGSizeMake(320 * 5, 436); UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(320 * 0, 0, 320, 436)]; image1.image = [UIImage imageNamed:[NSString stringWithFormat: @"Guide Page One.png"]]; [self.helpScrollView addSubview:image1]; UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(320 * 1, 0, 320, 436)]; image2.image = [UIImage imageNamed:[NSString stringWithFormat: @"Guide Page Two.png"]]; [self.helpScrollView addSubview:image2]; UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(320 * 2, 0, 320, 436)]; image3.image = [UIImage imageNamed:[NSString stringWithFormat: @"Guide Page Three.png"]]; [self.helpScrollView addSubview:image3]; UIImageView *image4 = [[UIImageView alloc] initWithFrame:CGRectMake(320 * 3, 0, 320, 436)]; image4.image = [UIImage imageNamed:[NSString stringWithFormat: @"Guide Page Four.png"]]; [self.helpScrollView addSubview:image4]; UIImageView *image5 = [[UIImageView alloc] initWithFrame:CGRectMake(320 * 4, 0, 320, 436)]; image5.image = [UIImage imageNamed:[NSString stringWithFormat: @"Guide Page Five.png"]]; [self.helpScrollView addSubview:image5]; self.helpScrollView.pagingEnabled = true; self.helpScrollView.showsHorizontalScrollIndicator = NO; 

我已经将UIPageControl拖放到我的xib文件的scrollview中,但是我不知道如何链接它们。 我该怎么做呢?

您不希望将UIPageControl添加到滚动视图本身,因为您不希望它滚动的内容。

这样说,看看UIScrollViewDelegate协议,特别是scrollViewDidEndDecelerating:当你的滚动视图停止移动时(这是更新你的UIPageControl的好时机)。

你的计算将会像..

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { self.pageControl.currentPage = floorf(scrollView.contentOffSet.x/320); } 

Re:在一个视图控制器中有两个滚动视图,那么这个视图指定我正在使用哪一个?

传入的scrollView实例可用于协商哪个滚动视图结束减速。 如果你为这两个滚动视图设置delegate属性,那么当这两个滚动视图中的任何一个都减速时,它将被调用,所以你需要validation哪个实例结束了减速。

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == self.helpScrollView) { self.pageControl.currentPage = floorf(scrollView.contentOffset.x/scrollView.frame.size.width); } }