自定义了一个UIImageView的子类
实现思路:
1.为UIImageView添加tap手势
2.在window的keywindow上添加一个透明的遮罩视图
3.tap时,在遮罩视图上创建一个与图片等比例的UIImageView,将图片等比例放大显示在这个imageVIew上,并为遮罩视图添加一个Tap手势用来返回
3.遮罩视图触发Tap时将遮罩视图隐藏
部分代码:
@implementation TapImageView
- (instancetype)init{
self = [super init];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picTapAction:)]];
return self;
}
- (void)picTapAction:(UIGestureRecognizer *)gr{
self.hidden = YES;
_backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_backView.backgroundColor = [UIColor blackColor];
[_backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapAction)]];
[[UIApplication sharedApplication].delegate.window addSubview:_backView];
_actionImageView = [[UIImageView alloc] initWithImage:self.image];
_actionImageView.frame = self.frame;
[[UIApplication sharedApplication].delegate.window addSubview:_actionImageView];
[UIView animateWithDuration:.3 animations:^{
CGFloat fixelW = CGImageGetWidth(_actionImageView.image.CGImage);
CGFloat fixelH = CGImageGetHeight(_actionImageView.image.CGImage);
_actionImageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, fixelH * [UIScreen mainScreen].bounds.size.width / fixelW);
_actionImageView.center = _backView.center;
}];
}
- (void)backTapAction{
[UIView animateWithDuration:.2 animations:^{
_actionImageView.frame = self.frame;
_backView.alpha = .3;
} completion:^(BOOL finished) {
[_backView removeFromSuperview];
[_actionImageView removeFromSuperview];
self.hidden = NO;
}];
}
@end