在此,封装了一个类,外面用的话直接调用方法,即可实现想要的效果。但是有一点,点进去之后只能查看对应的一张图,不能滑动,如果想要更多的效果,自己在此基础上进行再封装吧。只做参考。代码如下:
.h文件中:
#import
#import
@interface SJAvatarBrowser : NSObject
+(void)showImage:(UIImageView *)avatarImageView;
#import "SJAvatarBrowser.h"
static CGRect oldframe;
@implementation SJAvatarBrowser
+(void)showImage:(UIImageView *)avatarImageView{
UIImage *image=avatarImageView.image;
UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];
backgroundView.backgroundColor=[UIColor blackColor];
backgroundView.alpha=0;
UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];
imageView.image=image;
imageView.tag=1;
[backgroundView addSubview:imageView];
[window addSubview:backgroundView];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
[backgroundView addGestureRecognizer: tap];
[UIView animateWithDuration:0.3 animations:^{
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);
backgroundView.alpha=1;
} completion:^(BOOL finished) {
}];
}
+(void)hideImage:(UITapGestureRecognizer*)tap{
UIView *backgroundView=tap.view;
UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
[UIView animateWithDuration:0.3 animations:^{
imageView.frame=oldframe;
backgroundView.alpha=0;
} completion:^(BOOL finished) {
[backgroundView removeFromSuperview];
}];
}
////////////////////////////////
在此,一个封装好的类,已经结束。下面以ViewController为例,来说明:
#import "ViewController.h"
#import "SJAvatarBrowser.h"
@interface ViewController () {
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(100, 100, 300, 300);
imageView.image = [UIImage imageNamed:@"4.jpg"];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
_imageView = imageView;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage:)];
[imageView addGestureRecognizer:tap];
}
- (void)magnifyImage:(UITapGestureRecognizer *)gesture {
NSLog(@"版权所有,违者必究,Q_Q33757152的博客");
[SJAvatarBrowser showImage:_imageView];//调用方法
}