好久沒有更新艇拍,年齡越大人越懶罷,不廢話煞聪,上代碼
.h文件
////? ZJImageMagnification.h////? Created by apple on 2017/5/8.//? Copyright ? 2017年 apple. All rights reserved.//#import@interface ImageMagnification : NSObject/**
*? 瀏覽大圖
*
*? @param currentImageview 當前圖片
*? @param alpha? ? ? ? ? ? 背景透明度
*/
+(void)scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha;
@end
.m文件
////? ZJImageMagnification.m////? Created by apple on 2017/5/8.//? Copyright ? 2017年 apple. All rights reserved./*圖片放大*/#import "ImageMagnification.h"@implementation ImageMagnification//原始尺寸static CGRect oldframe;static CGRect newbounds;UIImageView *Imageview;float scale;/** *? 瀏覽大圖 * *? @param currentImageview 當前圖片 *? @param alpha? ? ? ? ? ? 背景透明度 */+(void)scanBigImageWithImageView:(UIImageView *)currentImageview alpha:(CGFloat)alpha {? ? //? 當前imageview的圖片? ? UIImage *image = currentImageview.image;? ? Imageview = currentImageview;? ? Imageview.hidden=YES;;? ? //? 當前視圖? ? 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所在視圖轉(zhuǎn)換到目標視圖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];? ? ? ? // 添加拖動手勢? ? UIPanGestureRecognizer*PanGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panImageView:)];? ? [backgroundView addGestureRecognizer:PanGestureRecognizer];? ? ? ? //縮放手勢? ? UIPinchGestureRecognizer*PinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchImageView:)];? ? [backgroundView addGestureRecognizer:PinchGestureRecognizer];? ? scale=1;? ? ? ? //? 動畫放大所展示的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ù)圖片寬高比設置? ? ? ? height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;? ? ? ? [imageView setFrame:CGRectMake(0, y, width, height)];? ? ? ? newbounds=imageView.bounds;? ? ? ? //重要逝慧! 將視圖顯示出來? ? ? ? [backgroundView setAlpha:1];? ? } completion:^(BOOL finished) {? ? ? ? ? ? }];? ? }/** *? 縮放imageView * *? @param pinch 縮放事件 */+ (void)pinchImageView:(UIPinchGestureRecognizer *)pinch{? ? UIView *backgroundView = pinch.view;? ? //? 原始imageview? ? UIImageView *imageView = [backgroundView viewWithTag:1024];? ? if (pinch.state==UIGestureRecognizerStateEnded) {? ? ? ? if (imageView.bounds.size.width!=newbounds.size.width) {? ? ? ? ? ? [UIView animateWithDuration:.4 animations:^{? ? ? ? ? ? ? ? imageView.center=CGPointMake(SCREEN_WIDTH/2, SCREENH_HEIGHT/2);? ? ? ? ? ? ? ? imageView.bounds=newbounds;? ? ? ? ? ? }];? ? ? ? }? ? ? ? return;? ? }? ? if (pinch.state == UIGestureRecognizerStateBegan || pinch.state == UIGestureRecognizerStateChanged) {? ? ? ? if (imageView.bounds.size.width*pinch.scale將背景視圖刪掉
Imageview.hidden=NO;
[UIView transitionWithView:Imageview duration:.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];
[backgroundView removeFromSuperview];
}];
}
return;
}
//返回在橫坐標上昔脯、縱坐標上拖動了多少像素
CGPoint point=[pan translationInView:backgroundView];
[UIView animateWithDuration:.1 animations:^{
imageView.center=CGPointMake(imageView.center.x+point.x, imageView.center.y+point.y);
if (imageView.center.y>SCREENH_HEIGHT/2) {
float alpha=(SCREENH_HEIGHT-imageView.center.y)/SCREENH_HEIGHT*2;
[backgroundView setAlpha:alpha];
imageView.bounds=CGRectMake(0, 0, oldframe.size.width+(newbounds.size.width-oldframe.size.width)*alpha, oldframe.size.height+(newbounds.size.height-oldframe.size.height)*alpha);
}else{
backgroundView.alpha=1;
imageView.bounds=newbounds;
}
}];
//拖動完之后,每次都要用setTranslation:方法制0這樣才不至于不受控制般滑動出視圖
[pan setTranslation:CGPointMake(0, 0) inView:backgroundView];
}
/**
*? 恢復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) {
//完成后操作->將背景視圖刪掉
Imageview.hidden=NO;
[UIView transitionWithView:Imageview duration:.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];
[backgroundView removeFromSuperview];
}];
}
@end
從入門到想放棄iOS笛臣,話說做這一行已經(jīng)4年云稚,從當初的實在沒辦法找工作學oc到現(xiàn)在一天不碰代碼就失眠,從沒有系統(tǒng)的學過iOS沈堡,感覺好可惜静陈,自己一點點摸索雖然成長的蠻快,但是真的很艱苦诞丽,希望能夠在簡書找到志同道合的朋友鲸拥,讓我們一起越走越遠。