ios cell展示可滑动的图片
需求:
点击cell上的图片.图片以原图显示出来,可以放大或缩小.再次点击图片移除图片显示原来界面.(和QQ空间看图片类似)

点击图片实现效果:

1. 自定义一个 UITableView (KDImageDetailTableView) 在方法(- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style)里面设置代理对象手势,tableView的背景色,隐藏指示器等
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
// Initialization code
//解决手势冲突(代理对象必须是UIScrollView类的对象)
self.panGestureRecognizer.delegate = self;
//设置代理对象
self.dataSource = self;
self.delegate = self;
//设置背景颜色
self.backgroundColor = [UIColor blackColor];
self.backgroundView = nil;
//去掉分割线
self.separatorStyle = UITableViewCellSeparatorStyleNone;
//设置横向
//1.逆时针旋转
self.transform = CGAffineTransformMakeRotation(-M_PI_2);
//2.重新设置frame
self.frame = frame;
//3.隐藏滑动指示器
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
//4.设置单元格的高度
self.rowHeight = kMainScreenWidth + 20;
//开启翻页效果
self.pagingEnabled = YES;
//默认隐藏
self.alpha = 0;
self.queue = [NSOperationQueue mainQueue];
}
return self;
}
当视图的内容在屏幕上绘制的时候
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.index inSection:0];
[self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
实现数据源方法和代理方法
#pragma mark - UITableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"imageDetailCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
self.showIndexpath = indexPath;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
//去掉单元格的背景颜色
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = nil;
//取消单元格的选中事件
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//顺时针旋转单元格
cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
//--------------创建子视图------------
//1.创建小的滑动视图(主要是为了缩放)
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, kMainScreenWidth, kMainScreenHeight)];
//tag
scrollView.tag = 201;
//取消弹性效果
scrollView.bounces = NO;
//设置缩放比例
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 2.0;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:scrollView];
//--------------创建图片---------
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight)];
// CGRectMake((kMainScreenWidth - 150)/2.0, (kMainScreenHeight -150)/2.0,150 , 150)];
//设置填充方式
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapAction:)];
tap.numberOfTouchesRequired = 1;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];
//tag
imageView.tag = 2014;
[scrollView addSubview:imageView];
UIActivityIndicatorView *aIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
aIndicatorView.center = CGPointMake(kMainScreenWidth/2.0, kMainScreenHeight/2.0);
aIndicatorView.hidden = YES;
aIndicatorView.tag = 2015;
[cell.contentView addSubview:aIndicatorView];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[self.queue cancelAllOperations];
NSString * imageUrl =self.dataList[indexPath.row];
NSURL * requestAddress = [[KDDataTools share] getThumbnailUrlWithPath:imageUrl size:CGSizeMake(960, 720)];
// NSURL * requestAddress = [NSURL URLWithString:imageUrl];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:2014];
imageView.frame = CGRectMake((kMainScreenWidth - 150)/2.0, (kMainScreenHeight -150)/2.0,150 , 150);
imageView.image = [self.thumbnailDic objectForKey:@(indexPath.row)];
UIScrollView * scrollView = (UIScrollView *)[cell.contentView viewWithTag:201];
UIActivityIndicatorView * aindicatorView =(UIActivityIndicatorView *) [cell.contentView viewWithTag:2015];
[aindicatorView startAnimating];
__weak UIImageView *weakImageView = imageView;
[imageView setImageWithURL:requestAddress placeholderImage:[self.thumbnailDic objectForKey:@(indexPath.row)] options:SDWebImageRetryFailed success:^(UIImage *image) {
// 1.把高清图片现实上去
weakImageView.image = image;
// 计算图片视图的高度 height / kscreenWith = size.heiht / size.with
float height = weakImageView.image.size.height /weakImageView.image.size.width * kMainScreenWidth;
height = MAX(height, kMainScreenHeight);
weakImageView.frame = CGRectMake(0, 0, kMainScreenWidth, height);
scrollView.contentSize = CGSizeMake(kMainScreenWidth, height);
[aindicatorView stopAnimating];
} failure:^(NSError *error) {
}];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self colse];
}
- (void)colse{
[UIView animateWithDuration:.35f animations:^{
self.alpha = 0;
}];
[self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:.35f];
}
- (void)imageTapAction:(UITapGestureRecognizer *)tap{
[self colse];
}
#pragma mark - UIGestureRecognizer Delegate
//下面的代理方法只要是YES所有的滑动手势都会响应
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)showInWindowFromsuperView:(UIView *)superView{
UIWindow *windown = [superView window];
[windown addSubview:self];
[UIView animateWithDuration:1 animations:^{
self.alpha = 1;
}];
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//如果滑动的事表示图(我不在执行下面代码)
if ([scrollView isMemberOfClass:[self class]]) {
return;
}
if (scrollView.contentOffset.x == 0 || scrollView.contentOffset.x + scrollView.width >= scrollView.contentSize.width) {
} else {
//获取滑动视图里面的手势对象,并获取位置
CGPoint point = [scrollView.panGestureRecognizer locationInView:self];
int index = point.y / 340;
//固定表示图
self.contentOffset = CGPointMake(0, index * 340);
}
}
这个方法返回的控件就是需要进行缩放的控件
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:2014];
return imageView;
}
ios cell展示可滑动的图片的更多相关文章
- iOS开发系列--无限循环的图片浏览器
--UIKit之UIScrollView 概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件 ...
- IOS 非常流畅的滑动tableView
为什么要写这篇文章呢?之前写过一篇,因为手机打字不是很方便,还有之前同事用6splus 定下午茶时候,我滑动列表时候竟然误以为是安卓系统的手机. tableview 流畅度可以用fps来测试,到6 ...
- ReactNative学习-滑动查看图片第三方组件react-native-swiper
滑动查看图片第三方组件:react-native-swiper,现在的版本为:1.4.3,该版本还不支持Android. 下面介绍的是该组件的一些用法,可能总结的不完整,希望大家一起来共同完善. 官方 ...
- iOS开发项目实战——Swift实现图片轮播与浏览
近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...
- IOS的H5页面滑动不流畅的问题:
IOS的H5页面滑动不流畅的问题: -webkit-overflow-scrolling : touch; 需要滑动的是哪块区域,就在哪里加上这段代码就OK
- H5+CSS3实现手指滑动切换图片
包含3个文件:html.slider-H5.js.jquery.js(自行下载).在html中可配置滑动参数.具体代码如下: HTML代码: <!DOCTYPE HTML> <htm ...
- Android:使用ViewPager实现左右滑动切换图片(图上有点点)
在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...
- Android:使用ViewPager实现左右滑动切换图片 (简单版)
ViewPager,它是google SDk中自带的一个附加包的一个类, 可以使视图滑动. 步骤: 1.引入android-support-v4.jar包,在主布局里加入 <android.su ...
- iOS WebView 加载本地资源(图片,文件等)
https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...
随机推荐
- 用数组求Fibonacci数列
#include<stdio.h>int main(){ int a[20]={1,1}; int n=2,i; for(n=2;n<20;n++) ...
- C++之map、list操作
#include <iostream> #include "map_struct.h" #include <map> using namespace std ...
- java中的方法重载与重写以及方法修饰符
1. 方法重载Overloading , 是在一个类中,有多个方法,这些方法的名字相同,但是具有不同的参数列表,和返回值 重载的时候,方法名要一样,但是参数类型和参数个数不一样,返回值类型可以相同,也 ...
- php正则
PHP代码 $str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="li ...
- Eclipse 语法提示
新建一个txt 拷贝下面的文本,然后保存修改扩展名为.epf #Sat Nov :: CST /instance/org.eclipse.jdt.core/org.eclipse.jdt.core.c ...
- Java BigDecimal使用
//除法:精确到后4位BigDecimal a = new BigDecimal(1213); BigDecimal b = new BigDecimal(10302); BigDecimal rat ...
- git分支的合并
原文: http://gitbook.liuhui998.com/3_3.html http://gitbook.liuhui998.com/5_3.html 一.如何分支的合并 在git中,可以使用 ...
- CSS position relative absolute fixed
position属性absolute与relative 详解 最近一直在研究javascript脚本,熟悉DOM中CSS样式的各种定位属性,以前对这个属性不太了解,从网上找到两篇文章感觉讲得很透彻 ...
- ModelState.IsValid
model内的设置如下所示: /// <summary> /// 取得或设置邮编 /// </summary> [RegularExpression(@"(^[1-9 ...
- 使用Axis2编写webservice客户端,服务端
1.编写客户端 Axis2开发WebService客户端 的3种方式 [参考帖子] http://blog.csdn.net/wangjinwei6912/article/details/851259 ...