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

时间:2013-10-19 02:25:03

标签: ios uiscrollview uipagecontrol

我正在创建一个应用程序,我需要使用UIPageControl滚动浏览一些图像。遗憾的是,我不知道如何使用UIPageControl或UIScrollView。非常感谢Apple提供的YouTube视频或Xcode文档链接!

提前致谢!!

3 个答案:

答案 0 :(得分:10)

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

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

请根据使用范围声明变量。我在本地声明了所有变量,这样可能不会混淆,并在视图控制器的底部添加UIPageControl

将UIScrollView的委托方法实现为当前的页面指示符

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
  {
     float 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

答案 1 :(得分:7)

<强>滚动型:

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

您可以浏览其中一些教程 Are there any good UIScrollView Tutorials on the net?

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

Scrollview的示例代码:

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;

向scrollview添加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; 

添加scrollview的委托方法,以在滚动时实现页面控制点

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

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

答案 2 :(得分:1)

这是Ray Wenderlich团队关于如何设置分页滚动视图的非常好的教程:How To Use UIScrollView to Scroll and Zoom Content

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

My answerthis previous question也可能对您有用。