#import <UIKit/UIKit.h>
@interface UIImageView (Utils) <UIGestureRecognizerDelegate>
- (void)showBigImageInWindow;
@end
#import "UIImageView+Utils.h"
@implementation UIImageView (Utils)
static CGRect oldframe;
- (void)showBigImageInWindow {
UIImageView *currentImageview = self;
UIImage *image = currentImageview.image;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
backgroundView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
oldframe = [currentImageview convertRect:currentImageview.bounds toView:window];
[backgroundView setAlpha:0];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
[imageView setUserInteractionEnabled:YES];
[imageView setImage:image];
[imageView setTag:10];
[backgroundView addSubview:imageView];
[window addSubview:backgroundView];
UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
pinch.delegate = self;
rotation.delegate = self;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
[backgroundView addGestureRecognizer:tapGestureRecognizer];
[UIView animateWithDuration:0.4 animations:^{
CGFloat y,width,height;
y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
width = [UIScreen mainScreen].bounds.size.width;
height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
[imageView setFrame:CGRectMake(0, y, width, height)];
[backgroundView setAlpha:1];
} completion:^(BOOL finished) {
}];
}
- (void)hideImageView:(UITapGestureRecognizer *)tap{
UIView *backgroundView = tap.view;
UIImageView *imageView = [tap.view viewWithTag:10];
[UIView animateWithDuration:0.4 animations:^{
[imageView setFrame:oldframe];
[backgroundView setAlpha:0];
} completion:^(BOOL finished) {
[backgroundView removeFromSuperview];
}];
}
-(void)panAction:(UIPanGestureRecognizer *)pan
{
UIImageView *imageView = (UIImageView *)pan.view;
if (!imageView) {
return ;
}
CGPoint position =[pan translationInView:imageView];
imageView.transform = CGAffineTransformTranslate(imageView.transform, position.x, position.y);
[pan setTranslation:CGPointZero inView:imageView];
}
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
UIImageView *imageView = (UIImageView *)pinch.view;
if (!imageView) {
return ;
}
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
-(void)rotationAction:(UIRotationGestureRecognizer *)rote
{
UIImageView *imageView = (UIImageView *)rote.view;
if (!imageView) {
return ;
}
imageView.transform = CGAffineTransformRotate(imageView.transform, rote.rotation);
rote.rotation = 0;
}
#pragma gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end