将Cell中的视图取出传递到根视图

当我们点击Cell中的某个图片时,图片会有一种从Cell中取出,放大,然后再回到原来的Cell中的效果。我的想法是:当Cell中的图片用button 来显示。当我们点击Cell中的这个button的时候,button触发方法以代理的方式将button的图片和这个Cell 传递到视图控制器。视图控制器通过坐标转换,得出图片在屏幕的位置坐标,然后保存这个位置并根据这个Frame在主视图创建一个ImageView。控制这个imageView的位置变化即可达到取出放大退回原有位置的效果。

//
//  MyCell.h
//  自定义Cell
//
//  Created by 邓竹立 on 15-3-18.
//  Copyright (c) 2015年 邓竹立. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol MyCellDelagate <NSObject>

-(void)image:(UIImage *)image rect:(CGRect) rect  from:(UIView*)view;

@end
@interface MyCell : UITableViewCell
@property (weak, nonatomic)  UIButton *iconButton;
@property(nonatomic,weak)id<MyCellDelagate> myDelgate;

+(instancetype)cellWithTableView:(UITableView *)tableView;


@end


//
//  MyCell.m
//  自定义Cell
//
//  Created by 邓竹立 on 15-3-18.
//  Copyright (c) 2015年 邓竹立. All rights reserved.
//

#import "MyCell.h"

@interface MyCell ()

@end

@implementation MyCell

- (void)clickIconButton:(UIButton *)sender
{
    [self.myDelgate image:sender.imageView.image rect:sender.frame from:self.contentView];
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *reuseId=@"cell";
    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell)
    {
        cell=[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    
    UIButton *button=[[UIButton alloc]init];
    
    cell.iconButton=button;
    button.frame=CGRectMake(0, 0, 120, 120);
    
    [cell.iconButton setImage:[UIImage imageNamed:@"DSC00003.jpg"] forState:UIControlStateNormal];
    [cell.iconButton addTarget:cell action:@selector(clickIconButton:) forControlEvents:
     UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];
    return  cell;
}

@end

 

#import "ViewController.h"
#import "MyCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,MyCellDelagate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.allowsSelection=NO;
}
-(void)image:(UIImage *)image rect:(CGRect)rect from:(UIView *)view
{
    UIImageView *imageView=[[UIImageView alloc] init];
    CGRect rect1=[view convertRect:rect toView:self.view];
    
    imageView.frame=CGRectMake(rect1.origin.x+100, rect1.origin.y+100, rect1.size.width, rect1.size.height);
    imageView.image=image;
    [self.view addSubview:imageView];
}


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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell=[MyCell cellWithTableView:tableView];
    cell.myDelgate=self;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



 

原文地址:https://www.cnblogs.com/dengzhuli/p/4415560.html