001.
002.
003.
004.
005.
006.
007.
008.
#import "ViewController.h"
009.
010.
@interface
ViewController ()
011.
{
012.
NSMutableArray
*myObject;
013.
014.
}
015.
@end
016.
017.
@implementation
ViewController
018.
019.
020.
021.
- (
void
)viewDidLoad
022.
{
023.
[
super
viewDidLoad];
024.
025.
026.
myObject = [[
NSMutableArray
alloc] init];
027.
028.
[myObject addObject:@
"ThaiCreate.Com 1"
];
029.
[myObject addObject:@
"ThaiCreate.Com 2"
];
030.
[myObject addObject:@
"ThaiCreate.Com 3"
];
031.
[myObject addObject:@
"ThaiCreate.Com 4"
];
032.
[myObject addObject:@
"ThaiCreate.Com 5"
];
033.
[myObject addObject:@
"ThaiCreate.Com 6"
];
034.
[myObject addObject:@
"ThaiCreate.Com 7"
];
035.
[myObject addObject:@
"ThaiCreate.Com 8"
];
036.
[myObject addObject:@
"ThaiCreate.Com 9"
];
037.
038.
039.
pageControlBeingUsed =
NO
;
040.
041.
042.
CGRect screenRect = [[
UIScreen
mainScreen] bounds];
043.
CGFloat screenWidth = screenRect.size.width;
044.
CGFloat screenHight = screenRect.size.height;
045.
046.
for
(
int
i = 0; i < [myObject count]; i++) {
047.
CGRect frame;
048.
frame.origin.x =
self
.scrollView.frame.size.width * i;
049.
frame.origin.y = 0;
050.
frame.size =
self
.scrollView.frame.size;
051.
052.
NSString
*val = [myObject objectAtIndex:i];
053.
054.
055.
UILabel
*txt=[[
UILabel
alloc] initWithFrame:CGRectMake(screenWidth*i,0,screenWidth ,screenHight)];
056.
txt.text = val;
057.
txt.textColor = [
UIColor
blackColor];
058.
txt.textAlignment =
NSTextAlignmentCenter
;
059.
060.
[
self
.scrollView addSubview:txt];
061.
062.
[txt release];
063.
}
064.
065.
066.
self
.scrollView.contentSize = CGSizeMake(
self
.scrollView.frame.size.width * [myObject count],
self
.scrollView.frame.size.height-100);
067.
068.
self
.pageControl.currentPage = 0;
069.
self
.pageControl.numberOfPages = [myObject count];
070.
071.
072.
}
073.
074.
- (
void
)scrollViewDidScroll:(
UIScrollView
*)sender {
075.
if
(!pageControlBeingUsed) {
076.
077.
CGFloat pageWidth =
self
.scrollView.frame.size.width;
078.
int
page =
floor
((
self
.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
079.
self
.pageControl.currentPage = page;
080.
}
081.
}
082.
083.
- (
void
)scrollViewWillBeginDragging:(
UIScrollView
*)scrollView {
084.
pageControlBeingUsed =
NO
;
085.
}
086.
087.
- (
void
)scrollViewDidEndDecelerating:(
UIScrollView
*)scrollView {
088.
pageControlBeingUsed =
NO
;
089.
}
090.
091.
- (
IBAction
)changePage {
092.
093.
CGRect frame;
094.
frame.origin.x =
self
.scrollView.frame.size.width *
self
.pageControl.currentPage;
095.
frame.origin.y = 0;
096.
frame.size =
self
.scrollView.frame.size;
097.
[
self
.scrollView scrollRectToVisible:frame animated:
YES
];
098.
099.
pageControlBeingUsed =
YES
;
100.
}
101.
102.
- (
void
)viewDidUnload {
103.
self
.scrollView =
nil
;
104.
self
.pageControl =
nil
;
105.
}
106.
107.
- (
void
)didReceiveMemoryWarning
108.
{
109.
[
super
didReceiveMemoryWarning];
110.
111.
}
112.
113.
- (
void
)dealloc {
114.
[_scrollView release];
115.
[_pageControl release];
116.
[
super
dealloc];
117.
}
118.
@end