如何实现一个图片浏览器

2018-02-12  本文已影响0人  galenu

实现效果

PhotoBrowser.gif

目录结构

image.png

调用方法

  1. 不带过渡动画,直接添加图片浏览器到window上
/**
 * 查看本地图片(不带过渡动画,直接加到window上)
 * urls 大图的url数组
 */
+ (PDPhotoBrowser *)photoWithImages:(NSArray *)images;

/**
 * 查看网络图片(不带过渡动画,直接加到window上)
 * urls 大图的url数组
 */
+ (PDPhotoBrowser *)photoWithUrls:(NSArray *)urls;

  1. 附带微信图片浏览器一样的过渡动画
/*
 * 查看本地图片(带过渡动画)
 * 用image数组快速创建图片浏览器
 * currentIndex 当前操作的下标
 * animateView 获取缩放返回的imageView的回调block
 */
+ (PDPhotoBrowser *)photoWithImages:(NSArray *)images currentIndex:(NSInteger)currentIndex animateView:(PDAnimateView)animateView;

/*
 * 查看网络图片(带过渡动画)
 * urls 大图的url数组
 * currentIndex 当前操作的下标
 * animateView 获取缩放返回的imageView的回调block
 */
+ (PDPhotoBrowser *)photoWithUrls:(NSArray *)urls currentIndex:(NSInteger)currentIndex animateView:(PDAnimateView)animateView;

实现原理

  1. 利用UICollectionView实现图片浏览
  2. UICollectionView的Cell底部是一个UIScrollView,在_scrollView上添加UIImageView用来展示图片,添加自定义的progressView用来显示加载进度
  3. UICollectionView的Cell添加单击手势退出图片浏览器,双击手势放大图片,长按手势弹出操作框
  4. 根据图片和屏幕比例关系,调整最大和最小伸缩比例,和换算长图的宽高
/**
 *  根据图片和屏幕比例关系,调整最大和最小伸缩比例
 */
- (void)setMaxAndMinZoomScales
{
    // self.photoImageView的初始位置
    UIImage *image = self.imageView.image;
    if (image == nil || image.size.height == 0) {
        return;
    }
    CGFloat imageWidthHeightRatio = image.size.width / image.size.height;
    
    CGRect imageViewFrame = CGRectZero;
    imageViewFrame.size.width = _scrollView.frame.size.width;
    imageViewFrame.size.height = _scrollView.frame.size.width / imageWidthHeightRatio;
    imageViewFrame.origin.x = 0;
    
    //垂直方向长图
    if (imageViewFrame.size.height > _scrollView.frame.size.height) {
        imageViewFrame.origin.y = 0;
        _scrollView.scrollEnabled = YES;
    } else {
         imageViewFrame.origin.y = (_scrollView.frame.size.height -  imageViewFrame.size.height ) * 0.5;
        _scrollView.scrollEnabled = NO;
    }
    
    self.imageView.frame = imageViewFrame;

    _scrollView.maximumZoomScale = MAX(_scrollView.frame.size.height / self.imageView.bounds.size.height, 3.0);
    _scrollView.minimumZoomScale = 1.0;
    _scrollView.zoomScale = 1.0;
    _scrollView.contentSize = CGSizeMake(imageViewFrame.size.width, MAX(imageViewFrame.size.height, _scrollView.frame.size.height));
}
  1. 添加打开图片浏览器和退出时的过渡动画
/**
 *  开始打开相册的过渡动画
 */
- (void)beginShowTransfromAnimation
{
    //获取到用户点击的那个UIImageView对象,进行坐标转化
    CGRect startRect;
    if (self.animateView) {
        _animateImageView = self.animateView(_currentIndex);
    }
    startRect = [self.animateImageView.superview convertRect:self.animateImageView.frame toView:self];
    
    //利用零时tempImageView完成过度的形变动画
    UIImageView *tempImageView = [[UIImageView alloc] init];
    
    UIImage *image = self.animateImageView.image;
    if (!image) {
        image = [UIImage imageNamed:@"pb_placeHolder"];
    }
    tempImageView.image = image;
    tempImageView.frame = startRect;
    [self addSubview:tempImageView];
    
    CGRect targetRect; // 目标frame
    CGFloat imageWidthHeightRatio = image.size.width / image.size.height;
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.width / imageWidthHeightRatio;
    CGFloat x = 0;
    CGFloat y;
    if (height > self.bounds.size.height) {
        y = 0;
    } else {
        y = (self.bounds.size.height - height ) * 0.5;
    }
    targetRect = CGRectMake(x, y, width, height);
    _collectionView.hidden = YES;
    self.alpha = 1.0;
    
    // 动画修改图片视图的frame,居中同时放大
    [UIView animateWithDuration:0.3 animations:^{
        tempImageView.frame = targetRect;
    } completion:^(BOOL finished) {
        [tempImageView removeFromSuperview];
        _collectionView.hidden = NO;
    }];
}

PDPhotoBrowser是基于collectionView实现的图片浏览器,详情请点击Demo地址

上一篇下一篇

猜你喜欢

热点阅读