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 ...
随机推荐
- POJ 3292 Semi-prime H-numbers
类似素数筛... Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6873 Accept ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDOJ 3652 B-number
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 北美IT公司大致分档
北美IT公司大致分档(from mitbbs.com) 第一档: Uber, Snapchat, Airbnb, Dropbox, Pinterest 第二档:Facebook, LinkedIn, ...
- 给Storyboard设置初始页面(Initial Controller)
原文:https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/SetInitialContr ...
- 开发板支持wifi
参考网址: http://wangye.org/blog/archives/845/ http://blog.csdn.net/lme525/article/details/37762519 htt ...
- 目前主流的Android定位有如下几种:
1.通过GPS模块 GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电:2,绝大部分用户默认不开启GPS模块:3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间:4,室内几乎无 ...
- Web项目,F12调试的说明
sessionstorage,localstorage和cookie之间的区别 区别:cookie数据始终在同源的http请求中携带(即使不需要),即cookie在浏览器和服务器间来回传递.而sess ...
- ubuntu linux 下wine的使用
ubuntu linux 下wine的使用 之前写了一篇 ubuntu15.10下编译安装wine1.8rc4 这一篇是来写它的使用的. 1.安装Wine支持包 现在,需要安装非开源(但免费)的支持包 ...
- 关于ubuntukylin安装后界面中英文混杂的问题
起因 一直使用的是ubuntu原版的系统,ubuntukylin出来后也没用使用过.一次去其论坛逛了一圈之后决定使用一下. 安装后的截面和ubuntu原版的差不多,还是挺漂亮的. 但是有一个问题是,安 ...