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

posted @ 2016-06-20 15:49  专注it  阅读(258)  评论(0编辑  收藏  举报