1. pageControl = [[UIPageControl alloc] init];
2. pageControl.frame = CGRectMake((self.view.bounds.size.width - 100) / 2.0,
3. self.view.frame.size.height - 40,
4. 100,
5. 20);
6. pageControl.numberOfPages = 3; // 一共有多少项(也就是有多少个圆点)
7. pageControl.pageIndicatorTintColor = [UIColor darkGrayColor]; // 指示器的颜色
8. pageControl.currentPageIndicatorTintColor = [UIColor cyanColor]; //指示器的当前颜色
9.
10. [self.view addSubview:pageControl];
11.
1.#import "ViewController.h"
2.
3.@interface ViewController ()<UIScrollViewDelegate>
4.{
5. UIScrollView *scrollView;
6. UIPageControl *pageControl;
7.}
8.@end
9.
10.@implementation ViewController
11.
12.- (void)viewDidLoad {
13. [super viewDidLoad];
14.
15.
16. scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
17. scrollView.bounces = NO;
18. scrollView.showsHorizontalScrollIndicator = NO;
19. scrollView.showsVerticalScrollIndicator = NO;
20.
21. CGFloat foreX = self.view.frame.size.width;
22. for (int i = 1; i <= 3; i++) {
23. NSString *str = [NSString stringWithFormat:@"%d.jpg",i];
24. UIImage *image = [UIImage imageNamed:str];
25. CGSize size = image.size;
26. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
27. imageView.frame = CGRectMake(foreX * (i - 1), 20, size.width, self.view.frame.size.height - 20);
28.
29. [scrollView addSubview:imageView];
30. }
31.
32. scrollView.contentSize = CGSizeMake(3 * foreX, self.view.frame.size.height - 20);
33.
34. [self.view addSubview:scrollView];
35.
36. scrollView.pagingEnabled = YES;// 启用翻页模式(不能会停在两个页面中间)
37.
38. scrollView.delegate = self;
39.
40. pageControl = [[UIPageControl alloc] init];
41. pageControl.frame = CGRectMake((self.view.bounds.size.width - 100) / 2.0,
42. self.view.frame.size.height - 40,
43. 100,
44. 20);
45. pageControl.numberOfPages = 3; // 一共有多少项(也就是有多少个圆点)
46. pageControl.pageIndicatorTintColor = [UIColor darkGrayColor]; // 指示器的颜色
47. pageControl.currentPageIndicatorTintColor = [UIColor cyanColor]; //指示器的当前颜色
48.
49. [self.view addSubview:pageControl];
50.
51.}
52.// 在scroll滚动到下一页后,改变pageControl的当前页
53.-(void)scrollViewDidScroll:(UIScrollView *)_scrollView{
54.
55. CGPoint offset = _scrollView.contentOffset;
56. CGFloat w = self.view.frame.size.width;
57.
58. pageControl.currentPage = (NSInteger)(offset.x / w);
59.
60.}
61.
62.
63.@end
原文:http://www.cnblogs.com/buakaw/p/5210106.html