`

tableView中图片点击全屏显示

阅读更多
//  tableView中的图片,点击后可以放大置全屏, 再点击缩小会原来位置
//

#import "HMViewController.h"

@interface HMViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) CGRect fristFrame; // 存储每次要展示的图片frame, 方便缩小时使用
@property (nonatomic, strong) UIImageView *fullImageView; // 全屏展示的视图
@property (nonatomic, weak) UITableView *tableView; // tableview
@end

@implementation HMViewController


// 懒加载全屏视图
- (UIImageView *)fullImageView
{
    if (_fullImageView == nil) {
        
        // 视图和屏幕一样大
        _fullImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        
        // 设置为可交互, 不然, 后面的手势根本不能用
        _fullImageView.userInteractionEnabled = YES;
        
        // 添加点击手势 ( 缩小图片时使用 )
        [_fullImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap2:)]];
        
        // 设置视图内容填充模式.
        _fullImageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    return _fullImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

/* 该方法中的注释代码为自定义imageView */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UIImageView *imageView;// 自定义ImageView,
    
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
//        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(25, 5, 30, 30)];
//        imageView.userInteractionEnabled = YES;
//        imageView.tag = 1; // 自定义imageView时方便获取
//        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTap:)]];
//        [cell.contentView addSubview:imageView];
        
        // cell的imageView设置为可交互
        cell.imageView.userInteractionEnabled = YES;
        // 添加点击手势 ( 放大图片时使用 )
        [cell.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTap:)]];
    }
    
    // 自定义imageView时使用
    //  imageView.image = [UIImage imageNamed:@"1"];
    
    cell.imageView.image = [UIImage imageNamed:@"1"];// 设置图片
    return cell;
}

/**
 *  图片放大
 */
-(void)actionTap:(UITapGestureRecognizer *)sender{
    
    // 根据点击手势的坐标,获取被点击的cell
    CGPoint loaction = [sender locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:loaction];
    UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
    // 自定义imageView时使用
    // UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:1];
    
    // 从cell中获取imageView
    UIImageView *imageView = cell.imageView;
    
    // 转换坐标系
    // 转换cell中的imageView的frame  在self.view中的坐标系
    CGRect newFrame = [imageView convertRect:imageView.bounds toView:self.view];

    // 保存被点击的图片的frame, 缩小时使用
    self.fristFrame = newFrame;
    
//    if (![self.fullImageView superview]) { // 貌似没用, 检查是否有父视图
    
        // 设置全屏视图中的图片
        self.fullImageView.image = imageView.image;
        
        // 设置frame起始位置(动画开始位置)
        self.fullImageView.frame = self.fristFrame;
        
        // 设置背景颜色
        self.fullImageView.backgroundColor = [UIColor blackColor];
        
        // 添加到视图上面
        [self.view addSubview:self.fullImageView];
        
        // 动画效果展示为全屏
        [UIView animateWithDuration:0.5 animations:^{
            self.fullImageView.frame = [UIScreen mainScreen].bounds;
        }];
        
//    }
    
    
}

/**
 *  图片缩小
 */
-(void)actionTap2:(UITapGestureRecognizer *)sender{
    
    // 清除背景颜色
    self.fullImageView.backgroundColor = [UIColor clearColor];
    
    // 缩小图片动画
    [UIView animateWithDuration:0.5 animations:^{
        self.fullImageView.frame = self.fristFrame; // 动画缩小到初始位置
    } completion:^(BOOL finished) {
        [self.fullImageView removeFromSuperview];// 从父视图中移除全屏视图
    }];
}



@end

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics