用uiscrollView和pageControl 实现翻页

转载自:http://blog.sina.com.cn/s/blog_6568e7880100l4kx.html

- (void)viewDidLoad {
    m_pageController.numberOfPages=30/5;
    [m_scrollView setContentSize:CGSizeMake(320*(30/6 +1), 331)];
     m_scrollView.pagingEnabled = YES;
    [m_scrollView setDelegate:self];
    for (int i = 0; i<30; i++) {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed: @"ViewCell" owner: self options: nil];
        ViewCell *catlg = [nib objectAtIndex: 0];
        NSInteger pageNum = i /6;
        CGRect frame = CGRectMake(0, 0,320,60);
        frame.origin = CGPointMake(320*pageNum, (i%6)*65+5);
        catlg.frame = frame;
        NSString *str=[[NSString alloc]initWithFormat:@"%d",i];
        catlg.m_label.text=str;
        
        
        [m_scrollView addSubview:catlg];
        
    }
    
    [super viewDidLoad];
}

- (IBAction)pageAction:(id)sender
{
    m_scrollView.contentOffset = CGPointMake(320 * [sender currentPage],0);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = m_scrollView.frame.size.width;
    int page = floor((m_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;   //这一句还不是理解,但实验确实成功了
    m_pageController.currentPage = page;
}

你可能感兴趣的:(用uiscrollView和pageControl 实现翻页)