美文网首页
OC类似微信图片展示工具

OC类似微信图片展示工具

作者: 史努比不是小孩_68fc | 来源:发表于2017-08-16 15:36 被阅读41次

好久没有更新,年龄越大人越懒罢,不废话,上代码

.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所在视图转换到目标视图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;        //高度 根据图片宽高比设置        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,话说做这一行已经4年,从当初的实在没办法找工作学oc到现在一天不碰代码就失眠,从没有系统的学过iOS,感觉好可惜,自己一点点摸索虽然成长的蛮快,但是真的很艰苦,希望能够在简书找到志同道合的朋友,让我们一起越走越远。

相关文章

网友评论

      本文标题:OC类似微信图片展示工具

      本文链接:https://www.haomeiwen.com/subject/bgjjcxtx.html