摘要:本文将带你了解IOS源码之实现图片点击全屏预览,希望本文对大家学IOS有所帮助。
//
//
ViewController.m // XWZoomImageView // // Created by xiao on 15/11/13. // Copyright © 2015年 xiao. All rights reserved. // #<d>import</d> "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIImageView *picView; @property (weak, nonatomic) UIScrollView *scrollView; @property (weak, nonatomic) UIImageView *lastImageView; @property (nonatomic, assign)CGRect original<d>Frame</d>; @property (nonatomic, assign)BOOL isDoubleTap; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.picView.userInteractionEnabled = YES; //添加单击手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)]; [self.picView addGestureRecognizer:tap]; } -(void)showZoomImageView:(UITapGestureRecognizer *)tap { if (![(UIImageView *)tap.view image]) { return; } //scrollView作为背景 UIScrollView *bgView = [[UIScrollView alloc] init]; bgView.<d>frame</d> = [UIScreen mainScreen].bounds; bgView.backgroundColor = [UIColor blackColor]; UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)]; [bgView addGestureRecognizer:tapBg]; UIImageView *picView = (UIImageView *)tap.view; UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = picView.image; imageView.<d>frame</d> = [bgView convertRect:picView.<d>frame</d> fromView:self.view]; [bgView addSubview:imageView]; [[[UIApplication sharedApplication] keyWindow] addSubview:bgView]; self.lastImageView = imageView; self.original<d>Frame</d> = imageView.<d>frame</d>; self.scrollView = bgView; //最大放大比例 self.scrollView.maximumZoomScale = 1.5; self.scrollView.delegate = self; [UIView animateWithDuration:0.5 animations:^{ CGRect <d>frame</d> = imageView.<d>frame</d>; <d>frame</d>.size.width = bgView.<d>frame</d>.size.width; <d>frame</d>.size.height = <d>frame</d>.size.width * (imageView.image.size.height / imageView.image.size.width); <d>frame</d>.origin.x = 0; <d>frame</d>.origin.y = (bgView.<d>frame</d>.size.height - <d>frame</d>.size.height) * 0.5; imageView.<d>frame</d> = <d>frame</d>; }]; } -(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer { self.scrollView.contentOffset = CGPointZero; [UIView animateWithDuration:0.5 animations:^{ self.lastImageView.<d>frame</d> = self.original<d>Frame</d>; tapBgRecognizer.view.backgroundColor = [UIColor clearColor]; } completion:^(BOOL finished) { [tapBgRecognizer.view removeFromSuperview]; self.scrollView = nil; self.lastImageView = nil; }]; } //返回可缩放的视图 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.lastImageView; }
最后同样带上一张图片吧,大致是这样子
再给大家分享一则IOS中点击图片后放大的代码
.h文件-----------------------------------------------------------------
#<d>import</d> @interface ZoomImage : NSObject /** *@brief点击图片放大,再次点击缩小 * *@param oldImageView 头像所在的imageView */ +(void)showImage:(UIImageView*)avatarImageView; @end .m文件----------------------------------------------------------------- #<d>import</d> "ZoomImage.h" static CGRect old<d>frame</d>; @implementation ZoomImage +(void)showImage:(UIImageView*)avatarImageView { UIImage *image =avatarImageView.image; // 获得根窗口 UIWindow *window =[UIApplication sharedApplication].keyWindow; UIView *backgroundView =[[UIView alloc]initWith<d>Frame</d>:CGRectMake(0, 0, WIDTH, HEIGHT)]; old<d>frame</d> =[avatarImageView convertRect:avatarImageView.bounds toView:window]; backgroundView.backgroundColor =[UIColor blackColor]; backgroundView.alpha =0.5; UIImageView *imageView =[[UIImageView alloc]initWith<d>Frame</d>:old<d>frame</d>]; imageView.image =image; imageView.tag =1; [backgroundView addSubview:imageView]; [window addSubview:backgroundView]; //点击图片缩小的手势 UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)]; [backgroundView addGestureRecognizer:tap]; [UIView animateWithDuration:0.3 animations:^{ imageView.<d>frame</d> =CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width); backgroundView.alpha =1; }]; } +(void)hideImage:(UITapGestureRecognizer *)tap{ UIView *backgroundView =tap.view; UIImageView *imageView =(UIImageView *)[tap.view viewWithTag:1]; [UIView animateWithDuration:0.3 animations:^{ imageView.<d>frame</d> =old<d>frame</d>; backgroundView.alpha =0; } completion:^(BOOL finished) { [backgroundView removeFromSuperview]; }]; }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号