简体   繁体   English

将UIPageControl添加到UIScrollView

[英]Add UIPageControl to UIScrollView

I have a UIScrollView containing 5 images. 我有一个包含5个图像的UIScrollView。 I want to add a UIPageControl at the bottom of my scroll view so that the user can see what page they are on. 我想在滚动视图的底部添加一个UIPageControl,以便用户可以看到它们所在的页面。

Here is my code for the scroll view: 这是滚动视图的代码:

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;

I have dragged a UIPageControl onto my scrollview in my xib file, but I don't know how to link them. 我已将UIPageControl拖到我的xib文件中的scrollview上,但我不知道如何链接它们。 How do I do this? 我该怎么做呢?

You don't want to add the UIPageControl to the scroll view itself because you don't want it to scroll with the content. 您不希望将UIPageControl添加到滚动视图本身,因为您不希望它与内容一起滚动。

With that said, take a look at the UIScrollViewDelegate protocol, specifically scrollViewDidEndDecelerating: which will be called when your scroll view stops moving (a good time to update your UIPageControl ). 有了这个,请看一下UIScrollViewDelegate协议,特别是scrollViewDidEndDecelerating:当滚动视图停止移动时将会调用它(更新UIPageControl的好时机)。

Your calculation is going to be something like.. 你的计算将会是......

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

Re: I have two scroll views within one view controller, so does this specify which one I'm using? Re:我在一个视图控制器中有两个滚动视图,所以它指定我正在使用哪一个?

The scrollView instance passed in can be used to negotiate which scroll view did end decelerating. 传入的scrollView实例可用于协商哪个滚动视图结束减速。 If you set the delegate property for both of your scroll views this will get called when either of those end decelerating so you'll need to verify which instance ended decelerating. 如果为两个滚动视图设置了delegate属性,当其中任何一个结束减速时,将调用此属性,因此您需要验证哪个实例结束减速。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM