1 //
2 // UITestViewController.m
3 // UITest
4 //
5
6 #import "UITestViewController.h"
7
8 UIScrollView *myScrollView;
9 UIPageControl *myPageControl;
10
11 @implementation UITestViewController
12
13 - (void)loadScrollViewWithPage:(UIView *)page
14 {
15 int pageCount = [[myScrollView subviews] count];
16
17 CGRect bounds = myScrollView.bounds;
18 bounds.origin.x = bounds.size.width * pageCount;
19 bounds.origin.y = 0;
20 page.frame = bounds;
21 [myScrollView addSubview:page];
22 }
23
24 - (void)createPages
25 {
26 CGRect pageRect = myScrollView.frame;
27
28 //create pages
29 UIView *page1 = [[UIView alloc] initWithFrame:pageRect];
30 page1.backgroundColor = [UIColor blueColor];
31 UIView *page2 = [[UIView alloc] initWithFrame:pageRect];
32 page2.backgroundColor = [UIColor redColor];
33 UIView *page3 = [[UIView alloc] initWithFrame:pageRect];
34 page3.backgroundColor = [UIColor greenColor];
35
36 //add to scrollview
37 [self loadScrollViewWithPage:page1];
38 [self loadScrollViewWithPage:page2];
39 [self loadScrollViewWithPage:page3];
40
41 //cleanup
42 [page1 release];
43 [page2 release];
44 [page3 release];
45 }
46
47 - (void)viewDidLoad {
48
49 [super viewDidLoad];
50
51 float pageControlHeight = 18.0;
52 int pageCount = 3;
53
54 CGRect scrollViewRect = [self.view bounds];
55 scrollViewRect.size.height -= pageControlHeight;
56
57 //create scrollview
58 myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
59 myScrollView.pagingEnabled = YES;
60 myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width * pageCount,1);
61 myScrollView.showsHorizontalScrollIndicator = NO;
62 myScrollView.showsVerticalScrollIndicator = NO;
63 myScrollView.delegate = self;
64
65 //create pageview
66 CGRect pageViewRect = [self.view bounds];
67 pageViewRect.size.height = pageControlHeight;
68 pageViewRect.origin.y = scrollViewRect.size.height;
69
70 myPageControl = [[UIPageControl alloc] initWithFrame:pageViewRect];
71 myPageControl.backgroundColor = [UIColor blackColor];
72 myPageControl.numberOfPages = pageCount;
73 myPageControl.currentPage = 0;
74 [myPageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
75
76 //create pages
77 [self createPages];
78
79 //add to main view
80 [self.view addSubview:myScrollView];
81 [self.view addSubview:myPageControl];
82
83 //cleanup
84 [myPageControl release];
85 [myScrollView release];
86 }
87
88 - (void)scrollViewDidScroll:(UIScrollView *)sender
89 {
90 CGFloat pageWidth = sender.frame.size.width;
91 int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
92 myPageControl.currentPage = page;
93 }
94
95 - (void)changePage:(id)sender
96 {
97 int page = myPageControl.currentPage;
98
99 // update the scroll view to the appropriate page
100 CGRect frame = myScrollView.frame;
101 frame.origin.x = frame.size.width * page;
102 frame.origin.y = 0;
103 [myScrollView scrollRectToVisible:frame animated:YES];
104 }
105
106 - (void)didReceiveMemoryWarning {
107 // Releases the view if it doesn't have a superview.
108 [super didReceiveMemoryWarning];
109
110 // Release any cached data, images, etc that aren't in use.
111 }
112
113 - (void)viewDidUnload {
114 // Release any retained subviews of the main view.
115 // e.g. self.myOutlet = nil;
116 }
117
118
119 - (void)dealloc {
120 [super dealloc];
121 }
122
123 @end