zoukankan      html  css  js  c++  java
  • ios开发图片点击放大

    图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现

    创建了一个专门用于放大图片的类,以下为.h文件

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface SJAvatarBrowser : NSObject  
    4. /** 
    5.  *  @brief  浏览头像 
    6.  * 
    7.  *  @param  oldImageView    头像所在的imageView 
    8.  */  
    9. +(void)showImage:(UIImageView*)avatarImageView;  
    10.   
    11. @end  

    以下为.m文件

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. #import "SJAvatarBrowser.h"  
    2. static CGRect oldframe;  
    3. @implementation SJAvatarBrowser  
    4. +(void)showImage:(UIImageView *)avatarImageView{  
    5.     UIImage *image=avatarImageView.image;  
    6.     UIWindow *window=[UIApplication sharedApplication].keyWindow;  
    7.     UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];  
    8.     oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];  
    9.     backgroundView.backgroundColor=[UIColor blackColor];  
    10.     backgroundView.alpha=0;  
    11.     UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];  
    12.     imageView.image=image;  
    13.     imageView.tag=1;  
    14.     [backgroundView addSubview:imageView];  
    15.     [window addSubview:backgroundView];  
    16.       
    17.     UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];  
    18.     [backgroundView addGestureRecognizer: tap];  
    19.       
    20.     [UIView animateWithDuration:0.3 animations:^{  
    21.         imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);  
    22.         backgroundView.alpha=1;  
    23.     } completion:^(BOOL finished) {  
    24.           
    25.     }];  
    26. }  
    27.   
    28. +(void)hideImage:(UITapGestureRecognizer*)tap{  
    29.     UIView *backgroundView=tap.view;  
    30.     UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];  
    31.     [UIView animateWithDuration:0.3 animations:^{  
    32.         imageView.frame=oldframe;  
    33.         backgroundView.alpha=0;  
    34.     } completion:^(BOOL finished) {  
    35.         [backgroundView removeFromSuperview];  
    36.     }];  
    37. }  
    38. @end  

    引入此类之后,为自己需要放大的imageView添加tap手势

    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage)];  
    2.   
    3.     [self.imageView addGestureRecognizer:tap];  
    [objc] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. - (void)magnifyImage  
    2. {  
    3.     NSLog(@"局部放大");  
    4.     [SJAvatarBrowser showImage:self.imageView];//调用方法  
    5. }  

    转载请声明源地址http://blog.csdn.net/u013082522/article/details/18445901

  • 相关阅读:
    四种常见系统架构介绍
    NC-UAP客户化开发-数据建模
    NC-UAP客户化开发-4.NC数据库持久化技术
    NC-UAP客户化开发-NC基础技术
    NC-UAP客户化开发-开发环境搭建
    Docker: 使用socket 代理
    Disk:磁盘管理之LVM和系统磁盘扩容
    Grafana & Graphite & Collectd:监控系统
    ELK:收集k8s容器日志最佳实践
    Virus:病毒查杀
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/5600750.html
Copyright © 2011-2022 走看看