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

本文介绍了一个简单的iOS图片放大查看解决方案,通过自定义类SJAvatarBrowser实现了图片点击放大及返回原视图的功能。该方案仅需引入一个类并设置一行代码即可轻松调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请声明源地址点击打开链接

图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.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(00, [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.youkuaiyun.com/u013082522/article/details/18445901
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值