2015-12-21 37 views
0

您好我是很新的iOS和在我的项目II已创建了一个UITableView与图像像我下面的屏幕确定这很好如何添加UIImageView的iOS中弹出块

而且这里的时候,我拍了拍的tableView图片我想用弹出块使用一个UIView来显示相关的行图像。

但使用我的代码图像不显示弹出UIView。请看我下面的屏幕图片没有添加UIView弹出块请帮我一些。

我的代码:

#import "imageTableViewController.h" 

@interface imageTableViewController()<UITableViewDataSource,UITableViewDelegate> 
{ 
    UITableView *mainTable; 
    NSMutableArray *imageArray; 
    UIButton *imageButton; 
    UIView *popUpView; 
    BOOL isFullScreen; 
    CGRect prevFrame; 
} 

@end 

@implementation imageTableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    imageArray = [[NSMutableArray alloc] initWithObjects:@"flower.jpeg",@"Bird.jpg",@"Browser.jpeg", nil]; 
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)]; 
    mainTable.dataSource = self; 
    mainTable.delegate = self; 
    [self.view addSubview:mainTable]; 

    popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)]; 
    popUpView.backgroundColor = [UIColor orangeColor]; 
    popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); 
    [mainTable addSubview:popUpView]; 

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)]; 
    imageView.image = [UIImage imageNamed:@"flower.jpeg"]; 
    [popUpView addSubview:imageView]; 

} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return imageArray.count; 
} 
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cell"; 
    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (Cell == nil) 
    { 
     Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
    imageButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; 
    [imageButton setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal]; 
    [imageButton addTarget:self action:@selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside]; 
    [Cell.contentView addSubview:imageButton]; 


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

-(void)imgToFullScreen:(UITapGestureRecognizer*)sender { 
    if (!isFullScreen) { 
     [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 
      popUpView.hidden = NO; 
      prevFrame = CGRectMake(30, 30, 25, 25); 
      [popUpView setFrame:CGRectMake(120, 50, 150, 150)]; 
     }completion:^(BOOL finished){ 
      isFullScreen = TRUE; 
     }]; 
     return; 
    } 
    else{ 
     [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 
      [popUpView setFrame:prevFrame]; 

     }completion:^(BOOL finished){ 
      isFullScreen = FALSE; 
      popUpView.hidden = YES; 
     }]; 
     return; 
    } 
} 

enter image description here

+0

有你,如果'检查[UIImage的imageNamed:@ “flower.jpeg”]'收益为零?尝试使imageView绿色的背景颜色或其他任何东西,看看它是否正确定位。 –

+0

我必须将imageView背景颜色设置为绿色吗? – Krish

+0

不仅UIimageview甚至标签和按钮也不添加弹出UIview – Krish

回答

1

看起来像它的发生是因为

popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); 

创建视图,并扩展它真的很小,也都子视图将被按比例缩小。然后在显示动画块你改变弹出的框架,但你必须改变鳞片状:

popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1); 

编辑,由于评价

嗯是的,这是罚款和一个小问题是未来@In .disee即 当我放大弹出块图像弹出像我上面的屏幕和 当我缩小图像必须缩小相关的 行附近,理解?

您当前的实现有几个问题,包括:

  • 你的架构不允许你所选单元格的索引路径 - 这将是一个问题,无论如何,所以其他中你必须做出这一部分。

这将是太难解释的话,所有你必须chage,所以我写代码给你)我几乎不改变你的代码,但它会很酷,如果你改变它的方式,我写我的,因为它更方便,苹果引导

我该怎么办: 1)创建细胞 2自定义类)新增能力的细胞说点什么通过委托模式泰伯维 3)当在单元格按钮用户点击 - 细胞告诉tableview,其中哪个按钮被按下 4)table view将单元格图像的框架转换为自己的坐标并显示弹出框

只需更换给您的一个代码:

#import "imageTableViewController.h" 


@class CustomCell; 
@protocol CustomCellDelegate <NSObject> 
- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell; 
@end 

@interface CustomCell : UITableViewCell 
@property (nonatomic, strong) NSString *imageName; 
@property (nonatomic, strong) UIButton *imageBtn; 
@property (nonatomic, weak) id <CustomCellDelegate> delegate; 
@property (nonatomic, assign, readonly) CGRect imageRect; 
@end 

@implementation CustomCell 
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self){ 
     [self setup]; 
    } 
    return self; 
} 

- (CGRect)imageRect { 
    return self.imageBtn.frame; 
} 

- (void)setup { 
    self.imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; 
    [self.imageBtn addTarget:self action:@selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.contentView addSubview:self.imageBtn]; 
} 

- (void)prepareForReuse { 
    [self.imageBtn setImage:nil forState:UIControlStateNormal]; 
} 

- (void)setImageName:(NSString *)imageName { 
    _imageName = imageName; 
    [self.imageBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 
} 

- (void)imgToFullScreen:(UIButton *)sender {//btw it's not UITapGestureRecognizer * 
    [self.delegate didSelectImageNamed:self.imageName fromCell:self]; 
} 

@end 


@interface imageTableViewController() 
<CustomCellDelegate> 
@end 

@implementation imageTableViewController { 
    UITableView *mainTable; 
    NSMutableArray *imageArray; 
    UIButton *imageButton; 
    UIView *popUpView; 
    BOOL isFullScreen; 

    CGRect prevFrame; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    imageArray = [[NSMutableArray alloc] initWithObjects:@"flower.jpeg",@"Bird.jpg",@"Browser.jpeg", nil]; 
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)]; 
    mainTable.dataSource = self; 
    mainTable.delegate = self; 
    [self.view addSubview:mainTable]; 

    popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)]; 
    popUpView.backgroundColor = [UIColor orangeColor]; 
    popUpView.hidden = YES; 
    [mainTable addSubview:popUpView]; 

    UIView * imageView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)]; 
    imageView.backgroundColor = [UIColor colorWithRed:255./255. green:0 blue:0 alpha:1]; 
    [popUpView addSubview:imageView]; 

} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return imageArray.count; 
} 
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cell"; 
    CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (Cell == nil) 
    { 
     Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     Cell.delegate = self; 
    } 
    Cell.imageName = [imageArray objectAtIndex:indexPath.row]; 

    return Cell; 
} 

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

-(void)imgToFullScreen:(NSString *)imageName fromRect:(CGRect)frame { 

    if (!isFullScreen) { 

     [popUpView setFrame:frame]; 
     [popUpView setHidden:NO]; 

     [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 
      [popUpView setFrame:CGRectMake(120, 50, 150, 150)]; 
     }completion:^(BOOL finished){ 
      isFullScreen = YES; 
      prevFrame = frame; 
     }]; 
     return; 
    } 
    else{ 
     [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{ 
      [popUpView setFrame:prevFrame]; 
     }completion:^(BOOL finished){ 
      isFullScreen = NO; 
      popUpView.hidden = YES; 
     }]; 
     return; 
    } 
} 


#pragma mark - CustomCellDelegate 

- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell { 
    CGRect imageFrame = cell.imageRect; 
    CGRect imageFrameGlobalCoord = [mainTable convertRect:imageFrame fromView:cell]; 

    [self imgToFullScreen:name fromRect:imageFrameGlobalCoord]; 
} 

@end 
+0

嗯是的,这很好,一个小问题来了@in.disee,当我放大弹出块图像弹出像我的屏幕上,当我缩小图像必须缩小相关行附近,理解? – Krish

+0

如果解决方案适合您 - 请将答案标记为正确 –

+0

好吧我标记为正确 – Krish