iOS开发笔记:实现点击图片放大全屏(可设置放大后的背景透明度)
2016-03-01 本文已影响13581人
degulade
刚转行iOS的搬砖工人,在此记录下这条路上的点点滴滴,共勉
- 最近遇到这个问题,单击图片—>让图片全屏,再次单击—>恢复原图。
使用方法:
调用.h文件中的scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha
方法即可:
下载地址:https://github.com/ShadowBryan/ImageMagnification
全屏/返回.gifPS:一口气补完了《一拳超人》,好困,现在已经是凌晨两点,明天还要早起上班,所以稍稍偷下懒,直接上代码
完整代码如下:
ZJImageMagnification.m
//
// ZJImageMagnification.m
//
// Created by degulade on 2017/4/21.
// Copyright © 2017年 degulade. All rights reserved.
/*图片放大*/
#import "ZJImageMagnification.h"
@implementation ZJImageMagnification
//原始尺寸
static CGRect oldframe;
/**
* 浏览大图
*
* @param currentImageview 当前图片
* @param alpha 背景透明度
*/
+(void)scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha {
// 当前imageview的图片
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)];
// 当前imageview的原始尺寸->将像素currentImageview.bounds由currentImageview.bounds所在视图转换到目标视图window中,返回在目标视图window中的像素值
oldframe = [currentImageview convertRect:currentImageview.bounds toView:window];
[backgroundView setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:alpha]];
// 此时视图不会显示
[backgroundView setAlpha:0];
// 将所展示的imageView重新绘制在Window中
UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
[imageView setImage:image];
imageView.contentMode =UIViewContentModeScaleAspectFit;
[imageView setTag:1024];
[backgroundView addSubview:imageView];
// 将原始视图添加到背景视图中
[window addSubview:backgroundView];
// 添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
[backgroundView addGestureRecognizer:tapGestureRecognizer];
// 动画放大所展示的ImageView
[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) {
}];
}
/**
* 恢复imageView原始尺寸
*
* @param tap 点击事件
*/
+(void)hideImageView:(UITapGestureRecognizer *)tap{
UIView *backgroundView = tap.view;
// 原始imageview
UIImageView *imageView = [tap.view viewWithTag:1024];
// 恢复
[UIView animateWithDuration:0.4 animations:^{
[imageView setFrame:oldframe];
[backgroundView setAlpha:0];
} completion:^(BOOL finished) {
//完成后操作->将背景视图删掉
[backgroundView removeFromSuperview];
}];
}
@end
ZJImageMagnification.h
//
// ZJImageMagnification.h
//
// Created by degulade on 2017/4/21.
// Copyright © 2017年 degulade. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZJImageMagnification : NSObject
/**
* 浏览大图
*
* @param currentImageview 当前图片
* @param alpha 背景透明度
*/
+(void)scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha;
@end