如何在我的应用程序中使用UIPageControl? [英] How do I use UIPageControl in my app?

查看:114
本文介绍了如何在我的应用程序中使用UIPageControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making an app where I need some images can be scrolled through using a UIPageControl. Sadly, I don't know how to use a UIPageControl, or a UIScrollView either. A link to a YouTube video or Xcode documentation by Apple would be much appreciated!

提前致谢!!

推荐答案

这里有一个代码可以帮助你

here is a code which will help you

 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中有图像数组

// let say you have array of image in 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

please declare the variables according to scope of use. i have declared all the variables locally so that might not get confuse and also add the UIPageControl at the bottom of your viewcontroller

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

Implement the delegate method of UIScrollView to the current Page indicator

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

where pageControl is UIPageControl added on bottom your viewcontroller

这篇关于如何在我的应用程序中使用UIPageControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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