iOS之图片点击看大图

先看效果图哈
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];

控制器查看图片
https://gitee.com/lanyingwei/codes/ym0zeonwc62fjgrs5lxb129

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,067评论 25 709
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 100,083评论 9 468
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,330评论 2 33
  • 谨以此篇文章献给成长中的自己。 4月1日是一个非常值得纪念的日子,不是因为愚人节,而是加入007不写就出局社群。 ...
    员子圆梦阅读 3,214评论 2 9
  • 2017年4月8日 多云 春雨滋润了萌芽 泥土裹着思念发酵 每一刻意料之外的成长 穿起某天幸福奇妙 我对着他微笑 ...
    鲜栗子阅读 1,247评论 0 0

友情链接更多精彩内容