类似微信朋友圈查看图片

当我们把图片展示时,很多时候都是需要点击查看。像朋友圈,评论这些。因为项目中使用到了这里也记录一下实现。

主要代码

//
//  PZExamineImage.m
//  朋友圈查看图片
//
//  Created by erongchuang on 2018/5/21.
//  Copyright © 2018年 erongchuang. All rights reserved.
//

#import "PZExamineImage.h"
#import "UIShortTapGestureRecognizer.h"
#define Wscreen [UIScreen mainScreen].bounds.size.width
#define Hscreen [UIScreen mainScreen].bounds.size.height
@interface PZExamineImage()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property (strong,nonatomic)UIPageControl  *pageControl;
@property (strong,nonatomic)NSArray        *imageArr;
@property (strong,nonatomic)NSArray        *frameArray;
@property (strong,nonatomic)UIScrollView   *imgScroll;
@property (strong,nonatomic)UIScrollView   *scrollView;
@property(strong,nonatomic)UIImageView * imageView;
@property(assign,nonatomic)CGRect fromFrame;
@end
@implementation PZExamineImage
-(instancetype)init
{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
    }
    return self;
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Wscreen + 20, Hscreen)];
        _scrollView.contentSize = CGSizeMake((Wscreen+20) * (self.imageArr.count), 0);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.tag = 666;
        _scrollView.delegate = self;
        for (int i = 0; i<self.imageArr.count; i++)
        {
            UIScrollView * imageviewScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(i*(Wscreen+20), 0, Wscreen, Hscreen)];
            imageviewScrollView.minimumZoomScale = 1;
            imageviewScrollView.maximumZoomScale = 2;
            imageviewScrollView.delegate = self;
            imageviewScrollView.showsVerticalScrollIndicator=  NO;
            imageviewScrollView.showsHorizontalScrollIndicator = NO;
            //点击
            UIShortTapGestureRecognizer * singleTap = [[UIShortTapGestureRecognizer alloc]initWithTarget:self action:@selector(makeScrollViewDismiss:)];
            [imageviewScrollView addGestureRecognizer:singleTap];
            //双击
            UIShortTapGestureRecognizer *doubleTap = [[UIShortTapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
            doubleTap.numberOfTapsRequired = 2;
            [imageviewScrollView addGestureRecognizer:doubleTap];
            
            [singleTap requireGestureRecognizerToFail:doubleTap];
            
            [_scrollView addSubview:imageviewScrollView];
            UIImage * image =  self.imageArr[i];
            CGFloat scale = image.size.width/image.size.height;
            CGFloat imageViewH  = Wscreen / scale;
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (Hscreen-imageViewH)/2, Wscreen, imageViewH)];
            imageView.tag = 555;
            imageView.image = image;
            [imageviewScrollView addSubview:imageView];
        }
       
    }
    return _scrollView;
}
//imgArr 传入需要展示的图片数组   index:当前选中的是哪个图片 frameArr是图片之前的frame数组
- (void)showImageWithImgArray:(NSArray *)imgArr selectIndex:(NSInteger)index  imageFrameArr:(NSArray *)frameArr
{
    self.imageArr = imgArr;
    self.frameArray = frameArr;
    self.pageControl.currentPage = index;
    UIImage * currentImage = imgArr[self.pageControl.currentPage];
    self.scrollView.contentOffset = CGPointMake((Wscreen+20) * self.pageControl.currentPage, 0);
    self.imgScroll = [[UIScrollView alloc]init];
    self.imageView = [[UIImageView alloc]initWithImage:currentImage];
    self.fromFrame = [frameArr[self.pageControl.currentPage] CGRectValue];
    self.imgScroll.frame = self.fromFrame;
    self.imageView.frame = self.imgScroll.bounds;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.clipsToBounds = YES;
    [self.imgScroll addSubview:self.imageView];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview: self.imgScroll];
    [[UIApplication sharedApplication].keyWindow addSubview:self.pageControl];
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat scale = currentImage.size.width/currentImage.size.height;//宽高比
        CGFloat imageH = Wscreen/scale;
        self.imgScroll.frame = CGRectMake(0, (Hscreen-imageH)/2, Wscreen, imageH);
        self.imageView.frame = self.imgScroll.bounds;
    } completion:^(BOOL finished) {
        [self addSubview:self.scrollView];
        [self.imgScroll setHidden:YES];
    }];
}
#pragma mark -- Method

- (void)makeScrollViewDismiss:(UITapGestureRecognizer *)tap
{
    //调整返回的图片
    self.imageView.image = self.imageArr[self.pageControl.currentPage];
    //调整返回的初始位置
    CGFloat scale = self.imageView.image.size.width/self.imageView.image.size.height;
    UIScrollView * imageScrollView = self.scrollView.subviews[self.pageControl.currentPage];
    CGFloat imageViewW = Wscreen * imageScrollView.zoomScale;
    CGFloat imageViewH = imageViewW / scale;
    self.imgScroll.frame = CGRectMake(0, imageViewH<=Hscreen?(Hscreen-imageViewH)/2:0, imageViewW, imageViewH);
    self.imageView.frame = self.imgScroll.bounds;
    self.imgScroll.contentOffset = imageScrollView.contentOffset;//跟原始的scroll吻合
    //调整返回的最终位置
    self.fromFrame = [self.frameArray[self.pageControl.currentPage] CGRectValue];
    self.imgScroll.hidden = NO;
    [self.pageControl removeFromSuperview];
    [self.scrollView removeFromSuperview];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.imgScroll.frame = self.fromFrame;
        self.imageView.frame =self.imgScroll.bounds;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.imgScroll removeFromSuperview];
    }];
}
- (void)doubleTap:(UITapGestureRecognizer *)gesture
{
    UIScrollView * imageScrollView  = (UIScrollView *)gesture.view;
    if (imageScrollView.zoomScale >1.0) {
        [imageScrollView setZoomScale:1.0 animated:YES];
    }
    else
    {
        CGPoint location = [gesture locationInView:imageScrollView];
        [imageScrollView zoomToRect:CGRectMake(location.x, location.y, 1, 1) animated:YES];
    }
}
#pragma mark -- UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.tag == 666) {
        self.pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x /(Wscreen+20));
    }
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return (UIImageView *)[scrollView viewWithTag:555];
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentSize.width);
    UIImageView * imageView = (UIImageView *)[scrollView viewWithTag:555];
    imageView.center = CGPointMake(scrollView.contentSize.width/2, scrollView.contentSize.height<Hscreen?Hscreen/2: scrollView.contentSize.height/2);
}

#pragma mark -- getter
-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, Hscreen-60, Wscreen, 20)];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
        _pageControl.numberOfPages = self.imageArr.count;
    }
    return _pageControl;
}

@end

demo地址

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,071评论 25 707
  • 3月23日 星期四 晴天 今天放学后,我在学校玩了很久妈妈才来接我,回家的路上,她带我和妹妹去蓝田镇镇府送...
    A叶瑞妹阅读 270评论 2 2
  • 三天超大量的学习内容人已经让我转不好 180°更别说360°了!可我那腻害的助推教练却做到了360°的翻转!(. ...
    晚霞中的范特西阅读 368评论 0 2
  • 是你借微风传递着思念 还是我带着思念闯进了梦 清水池旁、青草地上 你与老友回忆着生前过往 只说了最快乐的部分 我轻...
    jingyueyan阅读 92评论 0 0