ImageZoomView.h
@interface ImageZoomView : UIImageView {
CGRect initFrame;
}
/**
* 添加imageView点击放大功能
*
* @param imageView 图片视图对象
*/
+ (void)zoomImageWhenTap:(UIImageView *)imageView;
@end
ImageZoomView.m
@implementation ImageZoomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
initFrame = frame;
self.userInteractionEnabled = YES;
self.contentMode = UIViewContentModeScaleAspectFit;
self.backgroundColor = [UIColor blackColor];
}
return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.4 animations:^{
self.frame = initFrame;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
+ (void)zoomImageWhenTap:(UIImageView *)imageView {
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
[imageView addGestureRecognizer:tapGesture];
}
#pragma mark - 点击手势
+ (void)handleSingleFingerEvent:(UITapGestureRecognizer *)tapGesture {
UIWindow *myWindow = [[UIApplication sharedApplication] keyWindow];
CGRect fromRect = [tapGesture.view.superView convertRect:tapGesture.view.frame toView:myWindow];
ImageZoomView *zoomView = [[ImageZoomView alloc] initWithFrame:fromRect];
zoomView.image = [(UIImageView *)tapGesture.view image];
[myWindow addSubview:zoomView];
[UIView animateWithDuration:0.4 animations:^{
zoomView.frame = myWindow.frame;
}];
}
@end
只需调用+(void)zoomImageWhenTap:(UIImageView *)imageView
方法,就可以是imageView点击放大到全屏。
代码