Fork me on GitHub

用uiscrollView和pageControl 实现翻页

- (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;
}

posted on 2012-03-04 00:10  pengyingh  阅读(219)  评论(0)    收藏  举报

导航