iOS 仿微信图片浏览器

效果如下:

图片浏览器调用代码:

[FullScreenShowImageView showFromeView:imageView WithImageArray:images CurrentIndex:index];

一 图片浏览器实现核心思想:

1. 图片缩放

将图片添加到ScrollerView中, 再通过实现ScrollerView的代理方法及添加双击手势.实现图片缩放功能,代码如下

- (void)scaleAction:(UITapGestureRecognizer *)tapGestureRecognizer{
    UIScrollView *gestureView = (UIScrollView *)tapGestureRecognizer.view;
    if (gestureView.zoomScale > 1) {
        [gestureView setZoomScale:1 animated:YES];
    }else{
        [gestureView setZoomScale:2 animated:YES];
    }
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    if (scrollView != _backScrollView) {
        return scrollView.subviews.firstObject;
    }
    return nil;
}
2. 图片下拉缩小逐渐透明

(1) 给图片添加平移手势

UIPanGestureRecognizer *panGestureRecognizer = [UIPanGestureRecognizer.alloc initWithTarget:self action:@selector(panAction:)];
            panGestureRecognizer.delegate = self;
            [imageView addGestureRecognizer:panGestureRecognizer];

(2)为了防止图片平移手势与scrollView左右滑动冲突,实现平移手势的代理方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer{
 //返回 YES所有手势将会触发   
 return YES;
}

(3)通过判断平移坐标,在平移事件中处理平移手势触发效果

- (void)panAction:(UIPanGestureRecognizer *)panGestureRecognizer{
    if (!_isPan) return; //用于判断当前是否处于可以触发平移的状态(如:图片正处于放大状态则不允许触发平移事件)
    UIView *panView = panGestureRecognizer.view;
    CGPoint panPoint = [panGestureRecognizer translationInView:panView.superview];
    if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
       //平移手势状态结束后 通过判断纵向平移距离决定让浏览器消失还是让图片回弹到原来状态 
       __weak typeof(self)weakSelf = self;
        if (panPoint.y > 150) {
            [self dissmissAction];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                panView.width = ScreenWidth;
                panView.height = ScreenHeight;
                panView.center = self.center;
                weakSelf.backgroundColor = RGBA(0, 0, 0, 1);
            } completion:^(BOOL finished) {
                weakSelf.backScrollView.scrollEnabled = YES;
                weakSelf.isVPan = NO;
            }];
        }
    }else{
        if (panGestureRecognizer.numberOfTouches !=1) return;// (防止缩放时的两根手指与平移时的一根手指发生冲突)
        if (!_isVPan) { //用于判断 只有平移的纵向坐标改变时才能触发第一次平移 否则将会与左右滑动同时触发
            if (panPoint.y > 0) { //向下平移才能触发第一次平移
                self.backScrollView.scrollEnabled = NO; //触发平移后 不能左右滑动切换图片
                _isVPan = YES;
            }
        }else{
            if (panPoint.y > 0) {
              //通过纵向平移距离缩放图片宽高
                panView.width = ScreenWidth - panPoint.y*0.5;
                panView.height = panView.width*(ScreenHeight/ScreenWidth);
            }else{
                panView.width = ScreenWidth;
                panView.height = ScreenHeight;
            }
//平移同时改变图片位置
            panView.center = CGPointMake(self.center.x+panPoint.x, self.center.y+panPoint.y);
////通过纵向平移距离改变背景透明度
            self.backgroundColor = RGBA(0, 0, 0, 1-panPoint.y/ScreenHeight);
        }
    }
}
3 浏览器弹出动画
[view.superview convertRect:view.frame toView:self]
//将view.superview上的view.frame转化为self上的frame

通过这句代码可以获取其他父视图的子视图在当前父视图上的坐标

4 结构

外1层:View.
外2层:横向ScrollView.
外3层:用于缩放图片的ScrollView,每个里面放一张图片,有多少个图片就有多少个ScrollView容器
最里层:图片imageVIew

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中,滚动视图UIScrollView用于查看大于屏幕的内容。Scroll View有两个主要目的: 让用户...
    pro648阅读 38,456评论 4 37
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,538评论 1 14
  • 1 CALayer IOS SDK详解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi阅读 5,194评论 3 23
  • 敲敲打打了百来个字,最终还是没有什么好的思路写下去,又一个字一个字的删掉。 今天是连续写作的第21天,我还是不可避...
    墨尘禅阅读 483评论 2 1
  • 感恩老妈毫不犹豫帮我看孩子让我去开家长会,看俩孩子比一个麻烦,可老妈总是会毫不犹豫支持我。感恩结束回去的时候给妈打...
    寸心洁白阅读 130评论 0 1