如何在我的应用程序中使用UIPageControl?

我正在做一个应用程序,我需要一些图像可以通过使用UIPageControl滚动。 不幸的是,我不知道如何使用UIPageControl或者UIScrollView。 通过苹果的YouTubevideo或Xcode文档的链接将不胜感激!

提前致谢!!

这里是一个可以帮助你的代码

CGSize size = [[UIScreen mainScreen] bounds].size; CGFloat frameX = size.width; CGFloat frameY = size.height-30; //padding for UIpageControl UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(offsetX, offsetY, frameX, frameY)]; scrollView.pagingEnabled = YES; scrollView.backgroundColor = [UIColor clearColor]; scrollView.contentSize = CGSizeMake(frameX, frameY); [self.view addSubview: scrollView]; 

//假设你在imageArray中有图像数组

 for(int i = 0; i < [imageArray]; i++) { UIImage *image = [UIImage imageNamed:[imageArray objectAtIndex:i]]; imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = CGRectMake(frameX * i, 0.0, frameX, frameY); [scrollView addSubview:imageView]; } scrollView.contentSize = CGSizeMake(frameX*[imageArray count], frameY); 

请根据使用范围申报变数。 我已经在本地声明所有的variables,所以可能不会混淆,也添加在你的viewcontroller底部的UIPageControl

实现UIScrollView的委托方法到当前页面指示器

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { width = scrollView.frame.size.width; float xPos = scrollView.contentOffset.x+10; //Calculate the page we are on based on x coordinate position and width of scroll view pageControl.currentPage = (int)xPos/width; } 

其中pageControl是UIPageControl添加到您的viewcontroller的底部

滚动型:

ScrollView用于显示更多数量的视图/对象。 我们可以通过水平或垂直滚动​​显示这些视图。 您可以处理缩放,平移,滚动等

你可以通过一些这些教程在网上有没有什么好的UIScrollView教程?

https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

滚动视图的示例代码:

 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)]; //This will create a scrollview of device screen frame 

您可以启用滚动

  scroll.scrollEnabled = YES; 

为滚动视图添加3个视图的示例

  NSInteger numberOfViews = 3; for (int i = 0; i < numberOfViews; i++) { CGFloat xOrigin = i * self.view.frame.size.width; UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)]; awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; [scroll addSubview:awesomeView]; [awesomeView release]; } // 3 views added horizontally to the scrollview by using xOrigin. 

在这个最重要的部分是了解xOrigin。 这将把每一个UIView放在前一个UIView已经停止的地方,换句话说,每个UIView将在前一个UIView的末尾开始。 现在我们得到了3个视图滚动视图添加horrizontally。

设置UIScrollView contentSize

 scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 

contentSize只是三个UIViews的宽度的总和,如果每个UIView的宽度是320,而我们有三个UIViews,那么你的contentSize的宽度就是920。

 [self.view addSubview:scroll]; [scroll release]; //Add scrollview to viewcontroller 

页面控制:页面控件向用户显示一组代表页面的水平点。 当前页面显示为白色圆点。 用户可以从当前页面转到下一页面或前一页面。

要启用分页,您需要添加

 scroll.pagingEnabled = YES; pageControl = [[UIPageControl alloc] init]; //SET a property of UIPageControl pageControl.frame = CGRectMake(100,self.view.frame.size.height-100,self.view.frame.size.width-200,100); pageControl.numberOfPages = 3; //as we added 3 diff views pageControl.currentPage = 0; 

添加滚动视图的委托方法来实现滚动时的页面控制点

  - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = self.scrollView.frame.size.width; int page = floor((self.scrollView.contentOffset.x – pageWidth / 2 ) / pageWidth) + 1; //this provide you the page number pageControl.currentPage = page;// this displays the white dot as current page } 

现在你可以看到scrollview的pagecontrol。 希望你能理解。 请参阅https://developer.apple.com/library/ios/documentation/uikit/reference/UIPageControl_Class/Reference/Reference.html

这是Ray Wenderlich团队关于如何设置分页滚动视图的非常好的教程: 如何使用UIScrollView滚动和放大内容

Apple的文档也有一个如何实现分页滚动视图的例子: PhotoScroller

我对这个前面的问题的回答也许对你有用。