UIScrollView 实现 重用机制 dome

.h中的内容:

IBOutlet UIScrollView * scorllView;//
    UIView    *previousPage;
    UIView    *currentPage;
    UIView    *nextPage;
    IBOutlet UIPageControl * pageControl;//
    int pageCurrent;//
    NSMutableArray * mutableArray;//

.m中的内容:

- (void)viewDidLoad
{
    [super viewDidLoad];
    scorllView.contentSize=CGSizeMake(scorllView.frame.size.width*3, scorllView.frame.size.height);
    mutableArray =[[NSMutableArray alloc]init];
    [mutableArray addObject:[NSNull null]];
    [mutableArray addObject:[NSNull null]];
    [mutableArray addObject:[NSNull null]];
    [mutableArray addObject:[NSNull null]];
    [mutableArray addObject:[NSNull null]];
    [mutableArray addObject:[NSNull null]];


    scorllView.pagingEnabled = YES;
    scorllView.contentSize = CGSizeMake(320 * [mutableArray count], 372);
    scorllView.showsHorizontalScrollIndicator = NO;
    scorllView.showsVerticalScrollIndicator = NO;
    scorllView.scrollsToTop = NO;
    scorllView.delegate = self;
    [scorllView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
    previousPage = [[UIView alloc] initWithFrame:CGRectZero];
    currentPage = [[UIView alloc] initWithFrame:CGRectZero];
    nextPage = [[UIView alloc] initWithFrame:CGRectZero];
    previousPage.backgroundColor=[UIColor redColor];
    currentPage.backgroundColor=[UIColor blueColor];
    nextPage.backgroundColor=[UIColor greenColor];
    currentPage.frame = CGRectMake(pageCurrent * 320, 0, 320, 372);
    if (pageCurrent == 0) {
        previousPage.frame = CGRectZero;
        nextPage.frame = CGRectMake((pageCurrent + 1) * 320, 0, 320, 372);
    }else if (pageCurrent == [mutableArray count] - 1) {
        previousPage.frame = CGRectMake((pageCurrent - 1) * 320, 0, 320, 372);
        nextPage.frame = CGRectZero;
    }else {
        previousPage.frame = CGRectMake((pageCurrent - 1) * 320, 0, 320, 372);
        nextPage.frame = CGRectMake((pageCurrent + 1) * 320, 0, 320, 372);
    }
    CGPoint offset;
    offset.x = pageCurrent * 320;
    offset.y = 0;
    [scorllView setContentOffset:offset animated:NO];
    [scorllView addSubview:currentPage];
    [scorllView addSubview:nextPage];
    [scorllView addSubview:previousPage];
    [scorllView release];
    pageControl.numberOfPages=5;
    pageControl.currentPage=0;
    // Do any additional setup after loading the view from its nib.
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat pageWidth = scorllView.frame.size.width;
    int page = floor((scorllView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    UIView *tempPage = nil;
    if (pageCurrent == page) {  //  已经是第一页再往向前翻页,或者已经是最后一页再往后翻页。
        return;
    }else if (pageCurrent + 1 == page) { //  页面向后滑动了一页
        if (page > [mutableArray count] - 1) return;
        tempPage = currentPage;  
        currentPage = nextPage;  
        nextPage = previousPage; 
        previousPage = tempPage;
        previousPage.frame = CGRectMake((( page - 1) * 320), 0, 320, 372);
        currentPage.frame = CGRectMake(page * 320, 0, 320, 372);
        nextPage.frame = CGRectMake(((page + 1) * 320), 0, 320, 372);
        if (page == [mutableArray count] -1) {
            nextPage.frame = CGRectZero;
        }
        pageCurrent = page;
    }else if (pageCurrent - 1 == page) { //  页面向前滑动了一页
        if (page < 0) return;
        tempPage = currentPage;
        currentPage = previousPage;
        previousPage = nextPage;
        nextPage = tempPage;
        previousPage.frame = CGRectMake((page - 1) * 320, 0, 320, 372);
        currentPage.frame = CGRectMake(page *320, 0, 320, 372);
        nextPage.frame = CGRectMake((page + 1) * 320, 0, 320, 372);
        if (page == 0) {
            previousPage.frame = CGRectZero;
        }
        pageCurrent = page;
    }

}

你可能感兴趣的:(UIScrollView 实现 重用机制 dome)