将 UIPageControl 添加到 UIScrollView [英] Add UIPageControl to UIScrollView

查看:25
本文介绍了将 UIPageControl 添加到 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIScrollView containing 5 images. I want to add a UIPageControl at the bottom of my scroll view so that the user can see what page they are on.

这是我的滚动视图代码:

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;

我已将 UIPageControl 拖到我的 xib 文件中的滚动视图上,但我不知道如何链接它们.我该怎么做?

I have dragged a UIPageControl onto my scrollview in my xib file, but I don't know how to link them. How do I do this?

推荐答案

您不想将 UIPageControl 添加到滚动视图本身,因为您不希望它随内容一起滚动.

You don't want to add the UIPageControl to the scroll view itself because you don't want it to scroll with the content.

话虽如此,请查看 UIScrollViewDelegate 协议,特别是 scrollViewDidEndDecelerating: 将在您的滚动视图停止移动时调用(这是更新您的 代码>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).

您的计算将类似于..

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

回复:我在一个视图控制器中有两个滚动视图,那么这是否指定了我使用的是哪一个?

传入的 scrollView 实例可用于协商哪个滚动视图结束减速.如果您为两个滚动视图设置了 delegate 属性,这将在其中任何一个结束减速时被调用,因此您需要验证哪个实例结束减速.

The scrollView instance passed in can be used to negotiate which scroll view did end decelerating. 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.

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

这篇关于将 UIPageControl 添加到 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆