主屏幕只是一个简单UIScrollView
的UIPageControl
:)
例子 :
// MyViewController.h
@interface MyViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *_scrollView;
UIPageControl *_pageControl;
}
// MyViewController.m
// in your viewDidLoad method
int numberOfPages = 3;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
[_scrollView setContentSize:CGSizeMake(numberOfPages*_scrollView.frame.size.width, _scrollView.frame.size.height)];
[_scrollView setPagingEnabled:YES];
[_scrollView setShowsHorizontalScrollIndicator:NO];
[_scrollView setDelegate:self];
[self.view addSubview:_scrollView];
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height-30, _scrollView.frame.size.width, 20)];
[_pageControl setNumberOfPages:numberOfPages];
[_pageControl setCurrentPage:0];
[self.view addSubview:_pageControl];
for (int i=0; i<numberOfPages; i++) {
CGRect frame = _scrollView.frame;
frame.origin.x = frame.size.width * i;
frame.origin.y = 0;
UIView *view = [UIView alloc] initWithFrame:frame];
//Setup your view
[_scrollView addSubview:view];
}
如果要执行有关当前显示的视图的特定方法,请使用该scrollViewDidScroll:
方法(例如从数据库加载内容)
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
_pageControl.currentPage = page;
//do whatever you want..
}
这只是一个示例,向您展示如何做到这一点。如果您使用的是数据库,或者每个视图都不同,您应该以不同的方式设置视图或检索数据,但逻辑将是相同的......