点击cell中的图片进行放大浏览
2016-04-21 11:20
423 查看
通常我们在tableView的cell中加载图片后需要点击图片,对图片进行放大浏览,下面我将介绍具体实现方法,代码为swift,在xcode7.1中测试通过。
1.加载一个tableView,并在cell中显示网络上的一张图片。
2.点击后的手势处理,这里通过传递的参数获取点击的imageview,实现代码如下:
3.隐藏图片,浏览图片时点击图片后隐藏。
1.加载一个tableView,并在cell中显示网络上的一张图片。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //图片URL let imgurl = "http://www.qybike.net/data/attachment/forum/201508/23/215649bigoohqn8oh1p0qh.jpg"; //在cell中的imgview上显示网络上的图片,imgview可自己定义。 cell.imgview.sd_setImageWithURL(NSURL(string: imgurl), placeholderImage: nil) { (img, NSError, SDImageCacheType, url:NSURL!) -> Void in cell.imgview.image = img; //添加一个手势 cell.imgview.userInteractionEnabled = true; let signTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imgclick:") //将对象传递给动作 cell.imgview.addGestureRecognizer(signTap) signTap.view!.tag = indexPath.row } return cell }
2.点击后的手势处理,这里通过传递的参数获取点击的imageview,实现代码如下:
func imgclick(sender: UITapGestureRecognizer){ //取UIimageview. let imgview = sender.view as? UIImageView let url = imgview?.sd_imageURL() //取得图片的URL print(url) //绘制一个窗口 let window = UIApplication.sharedApplication().keyWindow let backgroundView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height)) backgroundView.backgroundColor = UIColor.blackColor() backgroundView.alpha = 0 //绘制一个Imageview用来显示图片。 let imageView = UIImageView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)) imageView.image = imgview?.image imageView.tag = 1 backgroundView.addSubview(imageView) window?.addSubview(backgroundView) //添加一个手势,点击图片后隐藏图片。 let hide = UITapGestureRecognizer(target: self, action: "hideImage:") imageView.userInteractionEnabled = true imageView.addGestureRecognizer(hide) UIView.animateWithDuration(0.3, animations:{ () in let vsize = UIScreen.mainScreen().bounds.size imageView.frame = CGRect(x:0.0, y: 0.0, width: vsize.width, height: vsize.height) //保持图片比例 imageView.contentMode = .ScaleAspectFit backgroundView.alpha = 1 }, completion: {(finished:Bool) in }) }
3.隐藏图片,浏览图片时点击图片后隐藏。
func hideImage(sender: UITapGestureRecognizer){ let backgroundView = sender.view as UIView? if let view = backgroundView{ UIView.animateWithDuration(0.3, animations:{ () in let imageView = view.viewWithTag(1) as! UIImageView imageView.alpha = 0 }, completion: {(finished:Bool) in view.alpha = 0 view.superview?.removeFromSuperview() view.removeFromSuperview() }) } }
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- upload上传单张图片
- 图片引发的溢出危机(图)
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 鼠标滑过缩略图时放大图片(纯Css)
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- C#实现图片加相框的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚
- 用css实现图片垂直居中的使用技巧
- C++实现读取图片长度和宽度
- Android实现图片异步加载并缓存到本地