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

Zen*_*ing 3 uiscrollview uipagecontrol ios

我正在制作一个应用程序,我需要一些图像可以使用UIPageControl滚动.遗憾的是,我不知道如何使用UIPageControl或UIScrollView.Apple的YouTube视频或Xcode文档链接将非常感谢!

提前致谢!!

Roc*_*ker 11

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

 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];
Run Code Online (Sandbox Code Playgroud)

//假设你在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); 
Run Code Online (Sandbox Code Playgroud)

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

将UIScrollView的委托方法实现到当前的Page指示器

- (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;   
  }
Run Code Online (Sandbox Code Playgroud)

其中pageControl是在viewcontroller底部添加的UIPageControl


Cin*_*ntu 7

滚动型:

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

你可以浏览一些这些教程 网上有什么好的UIScrollView教程吗?

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
Run Code Online (Sandbox Code Playgroud)

您可以启用滚动

    scroll.scrollEnabled = YES;
Run Code Online (Sandbox Code Playgroud)

向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.
Run Code Online (Sandbox Code Playgroud)

这方面最重要的部分是理解xOrigin.这将使每个UIView完全放在前一个UIView停止的位置,换句话说,每个UIView将从前一个UIView开始.现在我们得到了滚动视图,其中3个视图添加了horrizontally.

设置UIScrollView contentSize

scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
Run Code Online (Sandbox Code Playgroud)

contentSize只是三个UIViews宽度的总和,如果每个UIView的宽度是320,我们有三个UIViews,你的contentSize宽度将是920.

[self.view addSubview:scroll];
[scroll release];
//Add scrollview to viewcontroller
Run Code Online (Sandbox Code Playgroud)

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

要启用分页,您需要添加

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; 
Run Code Online (Sandbox Code Playgroud)

添加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
   }
Run Code Online (Sandbox Code Playgroud)

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