码迷,mamicode.com
首页 > 其他好文 > 详细

通过UIScrollView和UIPageControl结合制作相册

时间:2015-10-23 22:43:31      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

1、创建UIScrollView视图:

    UIScrollView *aScrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];

    aScrollView.backgroundColor = [UIColor redColor];

    [self.view addSubview:aScrollView];

    aScrollView.contentSize = CGSizeMake(CGRectGetWidth(aScrollView.bounds)*4, CGRectGetHeight(aScrollView.bounds));

    aScrollView.pagingEnabled = YES;

    aScrollView.delegate = self;

2、在aScrollView视图上添加4个scrollView(暂定有4张相片),给每一个scrollView添加一张图片:

    NSArray *images = @[@"001.jpg",@"002.jpg",@"003.jpg",@"004.jpg"];

    for (int i = 0; i < 4; i++) {

        UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(CGRectGetWidth(aScrollView.bounds)*i, 0, CGRectGetWidth(aScrollView.bounds), CGRectGetHeight(aScrollView.bounds))] autorelease];

        [aScrollView addSubview:scrollView];

        scrollView.minimumZoomScale = 0.3;

        scrollView.maximumZoomScale = 3;

        scrollView.delegate = self;

        scrollView.tag = 200 + i;

        UIImage *image = [UIImage imageNamed:images[i]];

        CGFloat width = CGRectGetWidth(aScrollView.bounds);

        CGFloat height = width * image.size.height / image.size.width;

        UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)] autorelease];

        [scrollView addSubview:imageView];

        imageView.image = image;

        imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, CGRectGetHeight(self.view.bounds) / 2);

        imageView.tag =100 + i;

}

3、创建UIPageControl控件:

    UIPageControl *pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0,    CGRectGetHeight(self.view.bounds)-80, CGRectGetWidth(self.view.bounds), 50)] autorelease];

    [self.view addSubview:pageControl];

    pageControl.numberOfPages = 4;

    pageControl.currentPage = 0;

    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];

    [pageControl addTarget:self action:@selector(handlePageAction:) forControlEvents:UIControlEventValueChanged];

4、实现UIScrollView视图和UIPageControl控件联动的方法:

- (void)handlePageAction:(UIPageControl *)sender{

    CGPoint offset = CGPointZero;

    offset.x = sender.currentPage * CGRectGetWidth(self.view.bounds);

    for (id object in self.view.subviews) {

        if ([object isKindOfClass:[UIScrollView class]]) {

            [(UIScrollView *)object setContentOffset:offset animated:YES];

            UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:200+sender.tag];

            scrollView.zoomScale = 1;

        }

    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSInteger index = scrollView.contentOffset.x / CGRectGetWidth(scrollView.bounds);

    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:300];

    UIScrollView *aScrollView = (UIScrollView *)[self.view viewWithTag:200+pageControl.currentPage];

    aScrollView.zoomScale = 1;   

    for (id object in self.view.subviews) {

        if ([object isKindOfClass:[UIPageControl class]]) {

            [(UIPageControl *)object setCurrentPage:index];

        }

    }

}

5、实现相片的缩放功能:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{

    for (int i = 0; i < 4; i++) {

        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100+i];

        CGFloat content_width = scrollView.contentSize.width;

        CGFloat content_height = scrollView.contentSize.height;

        CGFloat width = CGRectGetWidth(scrollView.bounds);

        CGFloat height = CGRectGetHeight(scrollView.bounds);

        CGFloat delta_x = width > content_width ? (width - content_width) / 2 : 0;

        CGFloat delta_y = height > content_height ? (height - content_height)/2 : 0;

        imageView.center = CGPointMake(content_width / 2 + delta_x, content_height / 2 + delta_y);

    }

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    return [scrollView viewWithTag:scrollView.tag - 100];

}

 

通过UIScrollView和UIPageControl结合制作相册

标签:

原文地址:http://www.cnblogs.com/sxsy-2015/p/4905732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!