セル内のビューの取り出しをルートビューに渡す

3410 ワード

セル内の画像をクリックすると、セルから取り出して拡大し、元のセルに戻る効果があります.私の考えは、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