UIPageControl 类提供了一行点来指示当前显示的是多页面视图的哪一页。点击当前颜色鲜艳的页面指示器的左边或右边,会触发 UIControlEventValueChanged 事件,
并启动设置为控件动作的任何方法。你可以通过调用currentPage 查询控件的新值,并通过调整 numberOfPage 属性设置可用的页面数。
代码 :使用UIPageControl 指示器
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define RSTRING(X) NSStringFromCGRect(X)
#define BASEHEIGHT 284.0f
#define NPAGES 3
- (void) pageTurn: (UIPageControl *) aPageControl
{
int whichPage = aPageControl.currentPage;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
sv.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
[UIView commitAnimations];
}
- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
CGPoint offset = aScrollView.contentOffset;
pageControl.currentPage = offset.x / 320.0f;
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.title = @"Image Scroller";
// Create the scroll view and set its content size and delegate
sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);
sv.pagingEnabled = YES;
sv.delegate = self;
int i = 0;
// Load in all the pages
for ( i;i < NPAGES; i++)
{
NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);
[sv addSubview:iv];
[iv release];
}
[self.view addSubview:sv];
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
}