美文网首页封装学无止境
iOS之图片点击看大图

iOS之图片点击看大图

作者: 凡尘一笑 | 来源:发表于2016-10-31 17:07 被阅读37次
    先看效果图哈
    seebigpicture.gif

    在.h文件中

    #import <UIKit/UIKit.h>
    
    @interface SsrSeeBigView : UIView
    
    - (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)Selectimage IndexString:(NSString *)IndexString;
    @end
    

    在.m文件中

    #import "LYWseeBigView.h"
    #import <UIImageView+WebCache.h>
    /*** 屏幕宽 ***/
    #define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
    /*** 屏幕高 ***/
    #define kScreenHeight ([UIScreen mainScreen].bounds.size.height)
    @interface LYWseeBigView ()
    @property (nonatomic, assign) UIView *background;//图片放大
    @property (nonatomic , strong) UIImageView *browseImgView;
    @end
    @implementation LYWseeBigView
    - (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)Selectimage IndexString:(NSString *)IndexString
    {
        if (self = [super initWithFrame:frame])
        {
            //创建一个黑色背景
            //初始化一个用来当做背景的View。
            UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
            _background = bgView;
            [bgView setBackgroundColor:[UIColor blackColor]];
            [self addSubview:bgView];
            
            //创建显示图像的视图
            //初始化要显示的图片内容的imageView
            UIImageView *browseImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight - 20)];
            browseImgView.contentMode = UIViewContentModeScaleAspectFit;
            self.browseImgView = browseImgView;
            //要显示的图片,即要放大的图片
            if (Selectimage) {
                _browseImgView.image = Selectimage;
            }
            //如果传递的是url
            if (IndexString) {
                [_browseImgView sd_setImageWithURL:[NSURL URLWithString:IndexString] placeholderImage:nil];
            }
            
        
            [bgView addSubview:browseImgView];
            
            browseImgView.userInteractionEnabled = YES;
            //添加点击手势(即点击图片后退出全屏)
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
            [browseImgView addGestureRecognizer:tapGesture];
            
            [self shakeToShow:bgView];//放大过程中的动画
        }
        return self;
    }
    -(void)closeView
    {
        [self removeFromSuperview];
    }
    //放大过程中出现的缓慢动画
    - (void) shakeToShow:(UIView*)aView
    {
        CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        animation.duration = 0.3;
        NSMutableArray *values = [NSMutableArray array];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        animation.values = values;
        [aView.layer addAnimation:animation forKey:nil];
    }
    

    简单使用

    传递图片
      _seeBigImage = [[SsrSeeBigView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) image:self.ImageOne.image IndexString:nil];
            [self.view addSubview:_seeBigImage];
    
    传递url
      _seeBigImage = [[SsrSeeBigView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) image:nil IndexString:self.dataSources[Index]];
            [self.view addSubview:_seeBigImage];
    

    相关文章

      网友评论

        本文标题:iOS之图片点击看大图

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