首页 > 代码库 > 使用UIScrollView和UIPageControl做一个可以用手势来切换图片的效果

使用UIScrollView和UIPageControl做一个可以用手势来切换图片的效果

利用UIScrollView的滚动效果来实现,先上图:




实现过程是:在viewController里先加入UIScrollView和UIPageControl:

-(void) loadView {
    [super loadView];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320,480)];
    UIPageControl* pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 438, 320, 42)];
    self.scrollView = scrollView;
    self.pageControl = pageControl;
    self.pageControl.opaque = 1;
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];

}


然后来初始化UIScrollView:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.contentList = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSMutableArray* controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0 ; i < kNumberOfPages; i++) {
        [controllers addObject:[NSNull null]];
    }
    
    self.viewControllers = controllers;
    self.scrollView.pagingEnabled = YES;
    
    //contentSize是内容的宽度
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*kNumberOfPages,
                                             self.scrollView.frame.size.height);

    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.scrollsToTop = NO;
    self.scrollView.delegate = self;
    
    self.pageControl.numberOfPages = kNumberOfPages;
    self.pageControl.currentPage = 0;
    
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
    // Do any additional setup after loading the view.
}

//UserImageViewController里只有一个UIImageView
-(void) loadScrollViewWithPage:(int)page{
    if (page < 0) {
        return;
    }else if(page >= kNumberOfPages) {
        //do some thing,like jump to other screen
        
        CalendarViewExample* calendarView = [[CalendarViewExample alloc] init];
        [self presentViewController:calendarView animated:YES completion:^{}];
        return;
    }
    
    UserImageViewController* controller = [self.viewControllers objectAtIndex:page];
    if ((NSNull*)controller == [NSNull null]) {
        controller = [[UserImageViewController alloc] init];
        [self.viewControllers replaceObjectAtIndex:page withObject:controller];
    }
    
    if (controller.view.superview == nil) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [self.scrollView addSubview:controller.view];
        
        
        NSString* name =[self.contentList objectAtIndex:page];
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
        controller.numberImage.image = [UIImage imageWithContentsOfFile:path];
       
        
    }
}


如果用手指来滚动的时候:

-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    if (pageControlUsed) {
        
        return;
    }
    
    CGFloat pageWidth = scrollView.frame.size.width;
    //page表示当前滚动哪一页的标识
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth)+1;
    
    self.pageControl.currentPage = page;
    
    [self loadScrollViewWithPage:page-1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page+1];
    
}

-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}

-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}

然后如果切换到下一页:

-(void) changePage:(id)sender{
    int page = self.pageControl.currentPage;
    NSLog(@"chanepage page=%d",page);
    
    [self loadScrollViewWithPage:page-1];
    [self loadScrollViewWithPage:page];
    
    
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width*page;
    frame.origin.y = 0;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    
    pageControlUsed = YES;
}
代码可以在http://download.csdn.net/detail/baidu_nod/7547659下载