啥也不说了,很简单,直接贴代码:
-(void)initUI
{
static int nPageCtrlH = 30;
CGRect rc = self.frame;
CGRect scrollRc = CGRectMake(0, 0, rc.size.width, rc.size.height - nPageCtrlH);
CGRect pageRc = CGRectMake(0, scrollRc.size.height, rc.size.width, nPageCtrlH);
m_scrollView = [[UIScrollView alloc]initWithFrame:scrollRc];
m_scrollView.pagingEnabled = YES;
m_scrollView.showsHorizontalScrollIndicator = NO;
m_scrollView.showsVerticalScrollIndicator = NO;
m_scrollView.scrollsToTop = NO;
m_scrollView.delegate = self;
[self addSubview:m_scrollView];
m_pageControl = [[UIPageControl alloc]initWithFrame:pageRc];
m_pageControl.currentPage = 0;
[m_pageControl addTarget:self action:@selector(onChangePage:) forControlEvents:UIControlEventValueChanged];
[self addSubview:m_pageControl];
}
-(void)onChangePage:(id)nSender
{
if(m_vcArray == nil) return;
int nPage = (int)m_pageControl.currentPage;
if(nPage < 0 || nPage >= m_vcArray.count) return;
m_bClickedPage = TRUE;
CGRect rc = m_scrollView.frame;
CGPoint pos = m_scrollView.frame.origin;
pos.x = rc.size.width * nPage;
pos.y = 0;
[m_scrollView setContentOffset:pos animated:TRUE];
}
-(void)addView:(UIView*)view
{
if(view == nil) return;
if(m_vcArray == nil)
{
m_vcArray = [[NSMutableArray alloc]init];
}
CGRect rc = m_scrollView.frame;
rc.origin.x = rc.size.width * m_vcArray.count;
rc.origin.y = 0;
view.frame = rc;
[m_scrollView addSubview:view];
[m_vcArray addObject:view];
int nw = rc.size.width;
int nh = rc.size.height;
m_scrollView.contentSize = CGSizeMake(nw * m_vcArray.count, nh);
m_pageControl.numberOfPages = m_vcArray.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if(m_bClickedPage) return;
CGFloat pageWidth = m_scrollView.frame.size.width;
int page = floor((m_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
m_pageControl.currentPage = page;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
m_bClickedPage = FALSE;
}