当前位置: 首页 > news >正文

iOS—仿微信单击放大图片

//
//  ImageZoomView.h
//  手势缩放图片
//
//  Created by strong on 16/4/7.
//  Copyright © 2016年 LYX. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ImageZoomView : UIView
- (instancetype)initWithFrame:(CGRect)frame andWithImage:(UIImageView *)imageview;
@end
//
//  ImageZoomView.m
//  手势缩放图片
//
//  Created by strong on 16/4/7.
//  Copyright © 2016年 LYX. All rights reserved.
//

#import "ImageZoomView.h"
static CGRect oldframe;
@interface ImageZoomView ()<UIScrollViewDelegate>

@end

@implementation ImageZoomView{
    UIScrollView *holderView;
    UIImageView *showImgView;
    BOOL isFirst;
}
- (instancetype)initWithFrame:(CGRect)frame andWithImage:(UIImageView *)imageview{
    if(self = [super initWithFrame:frame]){
        UIImage *image=imageview.image;
        holderView = [[UIScrollView alloc]initWithFrame:frame];
        holderView.backgroundColor=[UIColor blackColor];
        holderView.showsHorizontalScrollIndicator = NO; //水平
        holderView.showsVerticalScrollIndicator = NO; // 竖直
        holderView.scrollEnabled=YES;
        holderView.directionalLockEnabled = NO;
        holderView.bounces=NO;
        holderView.delegate=self;
        holderView.autoresizesSubviews=YES;
        holderView.maximumZoomScale=4;
        holderView.minimumZoomScale=1;
        [holderView setZoomScale:0.5 animated:NO];

        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        oldframe = [imageview convertRect:imageview.bounds toView:window];
        [holderView setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]];
        //此时视图不会显示
        [holderView setAlpha:0];
        //将所展示的imageView重新绘制
        showImgView = [[UIImageView alloc] initWithFrame:oldframe];
        [showImgView setImage:imageview.image];
        [showImgView setTag:0];
        [holderView addSubview:showImgView];
        [self addSubview:holderView];

        //动画放大所展示的ImageView
        [UIView animateWithDuration:0.4 animations:^{
            CGFloat y,width,height;
            y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
            //宽度为屏幕宽度
            width = [UIScreen mainScreen].bounds.size.width;
            //高度 根据图片宽高比设置
            height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
            [showImgView setFrame:CGRectMake(0, y, width, height)];
            //重要! 将视图显示出来
            [holderView setAlpha:1];
        } completion:^(BOOL finished) {
            
        }];
        
        
        UITapGestureRecognizer *doubleTapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTapGesture setNumberOfTapsRequired:2];
        [holderView addGestureRecognizer:doubleTapGesture];
        [self addSubview:holderView];
        
        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
        [singleTapGestureRecognizer setNumberOfTapsRequired:1];
        [holderView addGestureRecognizer:singleTapGestureRecognizer];
        [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGesture];
       
    }
    return self;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
    (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    showImgView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
                                     scrollView.contentSize.height * 0.5 + offsetY);
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return showImgView;
}

- (void)handleDoubleTap:(UIGestureRecognizer *)gesture{
    if(!isFirst){
        isFirst=YES;
        CGPoint pointInView = [gesture locationInView:holderView];
        CGFloat newZoomScale = holderView.zoomScale * 4.0f;
        newZoomScale = MIN(newZoomScale, holderView.maximumZoomScale);
        CGSize scrollViewSize =holderView.bounds.size;
        CGFloat w = scrollViewSize.width / newZoomScale;
        CGFloat h = scrollViewSize.height / newZoomScale;
        CGFloat x = pointInView.x - (w / 2.0f);
        CGFloat y = pointInView.y - (h / 2.0f);
        CGRect rectToZoomTo = CGRectMake(x, y, w, h);
        [holderView zoomToRect:rectToZoomTo animated:YES];
    }else{
        isFirst=NO;
        CGFloat newZoomScale = holderView.zoomScale / 4.0f;
        newZoomScale = MAX(newZoomScale, holderView.minimumZoomScale);
        [holderView setZoomScale:newZoomScale animated:YES];
    }
    
}

- (void)singleTap:(UITapGestureRecognizer*)tap{
    
    [UIView animateWithDuration:0.4 animations:^{
        [showImgView setFrame:oldframe];
        [holderView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [holderView removeFromSuperview];
        [self removeFromSuperview];
    }];

}

@end
 
//点击事件
ImageZoomView *img=[[ImageZoomView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, HEIGHT_SCREEN) andWithImage:imgView];
    //当前视图
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:img];

 

转载于:https://www.cnblogs.com/YuFly-lyx/p/6146170.html

相关文章:

  • 内核优化参数
  • webpack搭建前端一条龙服务
  • 敏捷开发Scrum
  • 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
  • redis学习笔记
  • 设计师必看的8个TED 演讲
  • [ solr入门 ] - 利用solrJ进行检索
  • hudson运行出现java.io.IOException Cannot run program的错误分析
  • shell之变量和引用
  • GIT教程
  • ios UIApplocation 中APP启动方式
  • 推送知识点2
  • 各种数据库连接字符串
  • Java程序,JDK的安装、环境的配置
  • 写一个简易的shell脚本(二)
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • angular学习第一篇-----环境搭建
  • CSS相对定位
  • extjs4学习之配置
  • Git学习与使用心得(1)—— 初始化
  • java多线程
  • Js基础知识(四) - js运行原理与机制
  • magento 货币换算
  • mysql外键的使用
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • Spark RDD学习: aggregate函数
  • spark本地环境的搭建到运行第一个spark程序
  • windows-nginx-https-本地配置
  • 读懂package.json -- 依赖管理
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 来,膜拜下android roadmap,强大的执行力
  • 限制Java线程池运行线程以及等待线程数量的策略
  • Linux权限管理(week1_day5)--技术流ken
  • Nginx实现动静分离
  • ​ArcGIS Pro 如何批量删除字段
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (1)Android开发优化---------UI优化
  • (4.10~4.16)
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (poj1.3.2)1791(构造法模拟)
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (三)uboot源码分析
  • (一)u-boot-nand.bin的下载
  • (转)负载均衡,回话保持,cookie
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • ***监测系统的构建(chkrootkit )
  • *1 计算机基础和操作系统基础及几大协议
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .net core使用ef 6