首页 > 代码库 > 简单相册的实现

简单相册的实现

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
    scrollView.tag = 100;
    scrollView.contentSize = CGSizeMake(320 * 6, [UIScreen mainScreen].bounds.size.height);
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    [scrollView release];
    
    for (int i = 0; i < 6; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, 568)];
        scroll.maximumZoomScale = 4.0;
        scroll.minimumZoomScale = 1;
        scroll.delegate = self;
        scroll.tag = 110 + i;
        [scrollView addSubview:scroll];
        [scroll release];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i + 1] ofType:@"jpg"]];
        imageView.tag = 101 + i;
        [scroll addSubview:imageView];
        [imageView release];
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, 528, 300, 20)];
    pageControl.tag = 120;
    pageControl.numberOfPages = 6;
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    pageControl.currentPage = 0;
    [pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
    [pageControl release];
    
}

//设置缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    switch (scrollView.tag) {
        case 110:
            return [scrollView viewWithTag:101];
        case 111:
            return [scrollView viewWithTag:102];
        case 112:
            return [scrollView viewWithTag:103];
        case 113:
            return [scrollView viewWithTag:104];
        case 114:
            return [scrollView viewWithTag:105];
        case 115:
            return [scrollView viewWithTag:106];
        default:
            break;
    }
    return nil;
}

//滑动pageControl
- (void)handlePageControl:(UIPageControl *)pageControl
{
    UIScrollView *scrollView =(UIScrollView *) [self.view viewWithTag:100];
    [scrollView setContentOffset:CGPointMake(320 * pageControl.currentPage, 0) animated:YES];
}

//恢复原图大小
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    switch (scrollView.tag) {
        case 100:{
            UIPageControl *pageControl =(UIPageControl *) [self.view viewWithTag:120];
            pageControl.currentPage = scrollView.contentOffset.x / 320;
            for (UIScrollView *scroll in scrollView.subviews) {
                if ([scroll isKindOfClass:[UIScrollView class]]) {
                    [scroll setZoomScale:1.0];
                }
            }
        }
            break;
        default:
            break;
    }
}

简单相册的实现