剛轉(zhuǎn)行iOS的搬磚工人较剃,在此記錄下這條路上的點點滴滴缚柏,共勉
- 最近遇到這個問題凑术,單擊圖片—>讓圖片全屏,再次單擊—>恢復(fù)原圖防嗡。
使用方法:
調(diào)用.h文件中的scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha
方法即可:
下載地址:https://github.com/ShadowBryan/ImageMagnification
PS:一口氣補完了《一拳超人》变汪,好困,現(xiàn)在已經(jīng)是凌晨兩點蚁趁,明天還要早起上班裙盾,所以稍稍偷下懶,直接上代碼
完整代碼如下:
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 當(dāng)前圖片
* @param alpha 背景透明度
*/
+(void)scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha {
// 當(dāng)前imageview的圖片
UIImage *image = currentImageview.image;
// 當(dāng)前視圖
UIWindow *window = [UIApplication sharedApplication].keyWindow;
// 背景
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
// 當(dāng)前imageview的原始尺寸->將像素currentImageview.bounds由currentImageview.bounds所在視圖轉(zhuǎn)換到目標(biāo)視圖window中他嫡,返回在目標(biāo)視圖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;
//高度 根據(jù)圖片寬高比設(shè)置
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) {
}];
}
/**
* 恢復(fù)imageView原始尺寸
*
* @param tap 點擊事件
*/
+(void)hideImageView:(UITapGestureRecognizer *)tap{
UIView *backgroundView = tap.view;
// 原始imageview
UIImageView *imageView = [tap.view viewWithTag:1024];
// 恢復(fù)
[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 當(dāng)前圖片
* @param alpha 背景透明度
*/
+(void)scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha;
@end