IOS中实现图片点击全屏预览

2016-02-19 10:39 19 1 收藏

下面是个超简单的IOS中实现图片点击全屏预览教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

【 tulaoshi.com - 编程语言 】

如果你感觉累,那就对了那是因为你在走上坡路。。这句话似乎有点道理的样子,时常提醒自己无论走到哪都不要忘记自己当初为什么出发。有时想想感觉有的东西可以记录一下,就把它记录下来吧,这次想写一下关于单张图片点击全屏预览的问题,网上查了一些大神写的有的功能确实很强大但自己暂时想要的只是简单的功能就好,还有些方法自己也没弄出想要的效果,最后写了一个比较简单的点击单张图片的全屏预览和双指捏合缩小放大,可能有时要对图片做一些处理,这里放大后只是显示同一张图片并未做处理,下面直接贴出代码

//// ViewController.m// XWZoomImageView//// Created by xiao on 15/11/13.// Copyright © 2015年 xiao. All rights reserved.//#import "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 originalFrame;@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.frame = [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.frame = [bgView convertRect:picView.frame fromView:self.view]; [bgView addSubview:imageView];  [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];  self.lastImageView = imageView; self.originalFrame = imageView.frame; self.scrollView = bgView; //最大放大比例 self.scrollView.maximumZoomScale = 1.5; self.scrollView.delegate = self;  [UIView animateWithDuration:0.5 animations:^{  CGRect frame = imageView.frame;  frame.size.width = bgView.frame.size.width;  frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);  frame.origin.x = 0;  frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;  imageView.frame = frame; }];}-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer{ self.scrollView.contentOffset = CGPointZero; [UIView animateWithDuration:0.5 animations:^{  self.lastImageView.frame = self.originalFrame;  tapBgRecognizer.view.backgroundColor = [UIColor clearColor]; } completion:^(BOOL finished) {  [tapBgRecognizer.view removeFromSuperview];  self.scrollView = nil;  self.lastImageView = nil; }];}//返回可缩放的视图-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return self.lastImageView;}

最后同样带上一张图片吧,大致是这样子

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

再给大家分享一则IOS中点击图片后放大的代码

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)
.h文件-----------------------------------------------------------------#import@interface ZoomImage : NSObject/** *@brief点击图片放大,再次点击缩小 * *@param oldImageView 头像所在的imageView */+(void)showImage:(UIImageView*)avatarImageView;@end.m文件-----------------------------------------------------------------#import "ZoomImage.h"static CGRect oldframe;@implementation ZoomImage+(void)showImage:(UIImageView*)avatarImageView{ UIImage *image =avatarImageView.image; // 获得根窗口 UIWindow *window =[UIApplication sharedApplication].keyWindow; UIView *backgroundView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; oldframe =[avatarImageView convertRect:avatarImageView.bounds toView:window]; backgroundView.backgroundColor =[UIColor blackColor]; backgroundView.alpha =0.5; UIImageView *imageView =[[UIImageView alloc]initWithFrame:oldframe]; 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.frame =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.frame =oldframe;  backgroundView.alpha =0;   } completion:^(BOOL finished) {  [backgroundView removeFromSuperview]; }];}

来源:http://www.tulaoshi.com/n/20160219/1595358.html

延伸阅读
标签: PHP
  <? //读取图像的类型 //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF function GetImageType($filename) {return (($imginfo=@getimagesize($filename))!=null ? $imginf...
标签: Java JAVA基础
[前言]: 图片验证在有关注册和发表留言经常用到.我在此用jsp写了个.供大家参考. 其中 com.sun.image 不是java的标准包.需要另外下载.相关地址为: http://java.sun.com/products/java-media/jai/ 1 . random.jsp (产生四位的随机字符,由0-9,a-z,A-Z构成.并把最终字符串放到session中保存以让后续页面验证真伪) 代码如下: ...
标签: word
Word2013如何快速实现全屏浏览和退出全屏   1、用Word2013打开一篇文档,单击标题栏右侧的功能区显示选项,选择自动隐藏功能区选项。 2、此时,文档窗口会自动全屏覆盖,阅读工具栏也被隐藏起来了。效果图如下: 3、如果我们想退出全屏浏览,可以单击右上角的功能区显示选项,选择显示选项卡和命令选项。 提...
预备知识 iOS处理屏幕上的触摸动作,主要涉及到以下几个方法: 代码如下: touchesBegan:withEvent:          //触摸屏幕的最开始被调用 touchesMoved:withEvent:         //移动过程中被调用 touchesEnded:withEvent:    &nbs...
标签: Web开发
html head   script language="javascript" var tipTimer; function locateObject(n, d) { //v3.0 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&id.forms...

经验教程

181

收藏

37
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部