点击图片,全屏显示,然后再点击屏幕一下,返回。
没啥难的,直接上代码:
//
// ViewController.m
// Demo-hehe
//
// Created by yyt on 16/4/25.
// Copyright © 2016年 yyt. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, self.view.bounds.size.width-40, 300)];
imageView.image = [UIImage imageNamed:@"11"];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
tapGesture.numberOfTapsRequired=1;
[imageView addGestureRecognizer:tapGesture];
}
//缩放图片
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
if (![(UIImageView *)tap.view image]) {
return;
}
UIView *bgView = [[UIView alloc] init];
bgView.frame = [UIScreen mainScreen].bounds;
bgView.backgroundColor = [UIColor blackColor];
[[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
UITapGestureRecognizer *tapBgView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
[bgView addGestureRecognizer:tapBgView];
//必不可少的一步,如果直接把点击获取的imageView拿来玩的话,返回的时候,原图片就完蛋了
UIImageView *tempImageView = (UIImageView*)tap.view;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:tempImageView.frame];
imageView.image = tempImageView.image;
[bgView addSubview:imageView];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = imageView.frame;
frame.size.width = bgView.frame.size.width;
frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
frame.origin.x = 0;
frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
imageView.frame = frame;
}];
}
-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
[tapBgRecognizer.view removeFromSuperview];
}
@end